类型=不可接受,状态=406生成XML的Spring Rest错误

类型=不可接受,状态=406生成XML的Spring Rest错误,rest,spring-boot,Rest,Spring Boot,我正在研究SpringREST,在我的SpringREST应用程序中,如果我尝试生成json,一切都可以。我可以在浏览器上看到它。没有错误 但如果我想生成XML,我就使用products=“application/XML”或products=MediaType.TEXT\u XML\u值和 我发现这个错误: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing t

我正在研究SpringREST,在我的SpringREST应用程序中,如果我尝试生成json,一切都可以。我可以在浏览器上看到它。没有错误

但如果我想生成XML,我就使用products=“application/XML”或products=MediaType.TEXT\u XML\u值和 我发现这个错误:

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
我的rest代码是:

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));
        }
    }
}
package-getExample;
导入java.util.ArrayList;
导入java.util.List;
导入org.springframework.http.MediaType;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RequestParam;
导入org.springframework.web.bind.annotation.RestController;
进口pojo.地址;
进口pojo.Person;
@RestController
公共类GetExampleController{
@RequestMapping(value=“getExample”,method=RequestMethod.GET,products=MediaType.TEXT\uXML\uvalue)
公共列表getExample1(@RequestParam(value=“personId”,defaultValue=“0”)字符串id){
List personList=新建ArrayList();
Person person1=新的人(“1”、“伊尔凯”、“居内尔”,
新地址(“Cennet Mah.”、“K.切克梅奇”、“斯坦布尔”、“蒂尔克耶”);
personList.add(person1);
Person person2=新人(“2”、“alican”、“akkuş”,
新地址(“Cennet Mah.”、“K.切克梅奇”、“斯坦布尔”、“蒂尔克耶”);
personList.add(person2);
Person person3=新人(“3”、“mustafa”、“demir”,
新地址(“Cennet Mah.”、“K.切克梅奇”、“斯坦布尔”、“蒂尔克耶”);
personList.add(person3);
如果(id等于(“0”)){
回归人格;
}
否则{
返回personList.subList(Integer.parseInt(id)-1,Integer.parseInt(id));
}
}
}

错误是什么?为什么我可以得到XML输出?如何解决此问题?

您需要添加
jackson dataformat xml
的依赖项:

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

com.fasterxml.jackson.dataformat
:
如果在上有Jackson XML扩展名(Jackson dataformat XML)
类路径,它将用于呈现XML响应,并且非常相同
我们用于JSON的示例将起作用

如果Jackson的XML扩展不可用,JAXB(默认情况下提供 在JDK中),将使用 [您的类]注释为@XmlRootElement

要让服务器呈现XML而不是JSON,您可能需要发送 Accept:text/xml标题(或使用浏览器)


谢谢-这正是我错过的。虽然SpringBootStarter包含了现成的xml支持,但它没有。