type=Not Acceptable, status=406 用于生成 XML 的 Spring Rest 错误 [英] type=Not Acceptable, status=406 Error In Spring Rest For Producing XML
问题描述
我正在研究 Spring Rest 和我的 Spring Rest 应用程序,如果我尝试生成 json 一切正常.我可以在浏览器上看到它.没有错误.
I'm working on Spring Rest and in my Spring Rest app, if I try to produce json everything is OK. I can see it on browser. There is no error.
但是如果我想生成 XML,我会使用生成 = "application/xml" 或生成=MediaType.TEXT_XML_VALUE 和我收到此错误:
But if I want to produce XML, I use produces = "application/xml" or produces=MediaType.TEXT_XML_VALUE and I getting this error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Oct 23 18:30:51 EEST 2016
There was an unexpected error (type=Not Acceptable, status=406).
Could not find acceptable representation
我的其余代码是:
package getExample;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pojo.Address;
import pojo.Person;
@RestController
public class GetExampleController {
@RequestMapping(value = "getExample",method=RequestMethod.GET,produces=MediaType.TEXT_XML_VALUE)
public List<Person> getExample1(@RequestParam(value = "personId", defaultValue = "0") String id) {
List<Person> personList = new ArrayList<>();
Person person1 = new Person("1", "ilkay", "günel",
new Address("Cennet Mah.", "K.Çekmece", "İstanbul", "TÜRKİYE"));
personList.add(person1);
Person person2 = new Person("2", "alican", "akkuş",
new Address("Cennet Mah.", "K.Çekmece", "İstanbul", "TÜRKİYE"));
personList.add(person2);
Person person3 = new Person("3", "mustafa", "demir",
new Address("Cennet Mah.", "K.Çekmece", "İstanbul", "TÜRKİYE"));
personList.add(person3);
if (id.equals("0")) {
return personList;
}
else {
return personList.subList(Integer.parseInt(id)-1, Integer.parseInt(id));
}
}
}
错误是什么?为什么我可以得到 XML 输出?我该如何解决这个问题?
What is the error? Why can I get XML output? How can I solve this?
推荐答案
需要添加jackson-dataformat-xml
的依赖:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
否则,您可以使用 JAXB 注释来注释您的 bean.
Otherwise, you may annotate your bean with JAXB annotations.
如果您有 Jackson XML 扩展名 (jackson-dataformat-xml)类路径,它将用于呈现 XML 响应和非常相同的我们用于 JSON 的示例会起作用.
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.
...
如果 Jackson 的 XML 扩展不可用,JAXB(默认提供)在 JDK 中)将被使用,并有额外的要求[你的班级] 注释为@XmlRootElement...
If Jackson’s XML extension is not available, JAXB (provided by default in the JDK) will be used, with the additional requirement to have [your class] annotated as @XmlRootElement...
...
要让服务器呈现 XML 而不是 JSON,您可能必须发送Accept: text/xml 标头(或使用浏览器).
To get the server to render XML instead of JSON you might have to send an Accept: text/xml header (or use a browser).
这篇关于type=Not Acceptable, status=406 用于生成 XML 的 Spring Rest 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!