# 71.2 编写XML REST服务

如果classpath下存在Jackson XML扩展（`jackson-dataformat-xml`），它会被用来渲染XML响应，示例和JSON的非常相似。想要使用它，只需为你的项目添加以下依赖：

```markup
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>
```

你可能还需要添加Woodstox的依赖，它比JDK提供的默认StAX实现快很多，并且支持良好的格式化输出，提高了namespace处理能力：

```markup
<dependency>
    <groupId>org.codehaus.woodstox</groupId>
    <artifactId>woodstox-core-asl</artifactId>
</dependency>
```

如果Jackson的XML扩展不可用，Spring Boot将使用JAXB（JDK默认提供），不过`MyThing`需要注解`@XmlRootElement`：

```java
@XmlRootElement
public class MyThing {
    private String name;
    // .. getters and setters
}
```

想要服务器渲染XML而不是JSON，你可能需要发送一个`Accept: text/xml`头部（或使用浏览器）。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cwiki-us.gitbook.io/spring-boot-reference-guide-zh/ix.-how-to-guides/71.-spring-mvc/71.2-write-an-xml-rest-service.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
