gpt4 book ai didi

java - Spring @RestController 不会生成 application/xml

转载 作者:数据小太阳 更新时间:2023-10-29 03:00:30 33 4
gpt4 key购买 nike

我有一个@RestController,方法如下

@RequestMapping(path = "/thing", method = RequestMethod.GET, 
produces = { MediaType.APPLICATION_XML_VALUE })
public List<Thing> listThings() {
return thingMapper.listThings();
}

但是当我在 header 中使用 Accept:application/xml 发出 GET 请求时,响应的 Content-Length 为 0,并且不会产生任何内容。我知道我的查询正在返回数据,如果我删除 produces 属性并发出一个普通的 get 请求,它会将数据作为 json 返回……我只是无法让它生成 xml。有什么想法吗?

编辑:我应该提到我正在使用 Spring Boot web starter

最佳答案

If you have the Jackson XML extension (jackson-dataformat-xml) on the classpath, it will be used to render XML responses and the very same example as we used for JSON would work. To use it, add the following dependency to your project:

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

参见 here

关于java - Spring @RestController 不会生成 application/xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44437523/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com