Java 406在REST和hibernate中检索详细信息时出错
我正在尝试使用REST和hibernate获取XML中国家的详细信息。但是当我点击下面的URL时,我得到了一个错误。我已经将请求中的header accepts正确设置为xmlJava 406在REST和hibernate中检索详细信息时出错,java,xml,spring,hibernate,rest,Java,Xml,Spring,Hibernate,Rest,我正在尝试使用REST和hibernate获取XML中国家的详细信息。但是当我点击下面的URL时,我得到了一个错误。我已经将请求中的header accepts正确设置为xml The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
The resource identified by this request is only capable of generating
responses with characteristics not acceptable according to the request
"accept" headers.
控制器
@RequestMapping(value = "/getAllCountries", method =
RequestMethod.GET,produces="application/xml",
headers = "Accept=application/xml")
public List<Country> getCountries() throws CustomerNotFoundException{
List<Country> listOfCountries = countryService.getAllCountries();
return listOfCountries;
}
服务
@Transactional
public List<Country> getAllCountries() {
return countryDao.getAllCountries();
}
@Transactional
公共列表getAllCountries(){
返回countryDao.getAllCountries();
}
刀
公共列表getAllCountries(){
会话会话=this.sessionFactory.getCurrentSession();
List countryList=session.createQuery(“来自国家”).List();
返回国家列表;
}
有人能帮忙吗?在
pom.xml
中使用spring推荐的jackson dataformat xml
库。一旦JAXB库(内置于JDK>=1.6中)出现,即使没有XML注释,它也会为您自动进行XML转换。但是,您可以使用@JacksonXml..
注释为xml指定所需的结构
为了在这里获得所需的结果,我将创建一个包装器类并更新我的控制器,如下所示:
//pom.xml
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
//wrapper class
@JacksonXmlRootElement(localName = "countries")
@Data //lombok
@AllArgsConstructor //lombok
public class Countries {
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "country")
private List<Country> countries;
}
//controller
@RequestMapping(value = "/getAllCountries", method = RequestMethod.GET)
public Object getCountries() throws CustomerNotFoundException{
return new Countries(countryService.getAllCountries());
}
//pom.xml
com.fasterxml.jackson.dataformat
jackson数据格式xml
//包装类
@JacksonXmlRootElement(localName=“countries”)
@数据//龙目
@AllArgsConstructor//lombok
公营国家{
@JacksonXmlElementWrapper(useWrapping=false)
@JacksonXmlProperty(localName=“country”)
私人名单国家;
}
//控制器
@RequestMapping(value=“/getAllCountries”,method=RequestMethod.GET)
公共对象getCountries()引发CustomerNotFoundException{
返回新的国家(countryService.getAllCountries());
}
注意:此处不需要XML包装类。Spring只需使用
对默认数组进行转换即可,但是建议您按照所需的结构来塑造XML。您的依赖项中是否有Jackson
?@Thomas是的,我的依赖项中有Jackson。是否有其他方法来执行此操作?我已用更优雅的方法更新了解决方案以获得结果。它解决了您的查询吗?你能达到预期的结果吗?请看。
public List<Country> getAllCountries() {
Session session = this.sessionFactory.getCurrentSession();
List<Country> countryList = session.createQuery("from Country").list();
return countryList;
}
//pom.xml
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
//wrapper class
@JacksonXmlRootElement(localName = "countries")
@Data //lombok
@AllArgsConstructor //lombok
public class Countries {
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "country")
private List<Country> countries;
}
//controller
@RequestMapping(value = "/getAllCountries", method = RequestMethod.GET)
public Object getCountries() throws CustomerNotFoundException{
return new Countries(countryService.getAllCountries());
}