如何使用Spring Boot Rest Data返回XML

KRi*_*ico 3 xml rest json spring-data-rest spring-boot

我需要Spring Boot的输出,Spring Data REST是XML,而不是JSON。我放入存储库中:

@RequestMapping(value="/findByID", method=RequestMethod.GET, headers = { "Accept=application/xml" }, produces="application/xml")
MyXmlAnnotatedObject findById(@Param("id") BigInteger id);
Run Code Online (Sandbox Code Playgroud)

我还在pom依赖项中添加了以下内容

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
        <version>2.4.3</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.woodstox</groupId>
        <artifactId>woodstox-core-asl</artifactId>
        <version>4.4.1</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

但是当我尝试

http://localhost:9000/factset/search/findByID?id=18451
Run Code Online (Sandbox Code Playgroud)

我仍然得到JSON。我真的为我的用户需要XML任何想法吗?

Ily*_*nov 5

RequestMapping注释不适用于存储库。存储库方法不允许您更改结果格式(默认为JSON)。如果您希望服务以XML格式返回数据,则需要创建简单的@Controller。

@Controller
public class RestEndpoint {

    @Autowired
    private SomeRepository someRepository;

    @RequestMapping(value="/findByID", method=RequestMethod.GET, produces=MediaType.APPLICATION_XML_VALUE)
    public @ResponseBody MyXmlAnnotatedObject findById(@Param("id") BigInteger id) {

        return someRepository.findById(id);
    }

}
Run Code Online (Sandbox Code Playgroud)

UPD:这是Spring官方文档的链接:http : //docs.spring.io/spring-data/rest/docs/2.1.4.RELEASE/reference/html/repository-resources.html

**3.6 The query method resource**
The query method resource executes the query exposed through an individual query method on the repository interface.

**3.6.1 Supported HTTP methods**

As the search resource is a read-only resource it supports GET only.

**GET**

Returns the result of the query execution.

**Parameters**

If the query method has pagination capabilities (indicated in the URI template pointing to the resource) the resource takes the following parameters:

page - the page number to access (0 indexed, defaults to 0).
size - the page size requested (defaults to 20).
sort - a collection of sort directives in the format ($propertyname,)+[asc|desc]?.
**Supported media types**

application/hal+json
application/json
Run Code Online (Sandbox Code Playgroud)