简体   繁体   English

检索REST和休眠中的详细信息时出现406错误

[英]406 error while retrieving the details in REST and hibernate

I am trying to fetch the details of countries in XML using REST and hibernate. 我正在尝试使用REST和休眠方式获取XML国家/地区的详细信息。 But when hitting the URL below is the error I get. 但是当点击下面的URL是我得到的错误。 I have set the header accepts in the request correctly to xml. 我已将请求中的标头接受设置为xml。

The resource identified by this request is only capable of generating 
responses with characteristics not acceptable according to the request 
"accept" headers.

CONTROLLER CONTROLLER

@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;
}

MODEL 模型

@XmlRootElement (name = "COUNTRY")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name="COUNTRY")
public class Country{

@XmlAttribute
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
int id;

@XmlElement
@Column(name="countryName")
String countryName; 

@XmlElement
@Column(name="population")
long population;

public Country() {
super();
}

SERVICE 服务

@Transactional
public List<Country> getAllCountries() {
    return countryDao.getAllCountries();
}

DAO DAO

public List<Country> getAllCountries() {
    Session session = this.sessionFactory.getCurrentSession();
    List<Country> countryList = session.createQuery("from Country").list();
    return countryList;
}

Can someone please help.. 有人可以帮忙吗..

Use spring recommended jackson-dataformat-xml library in pom.xml . pom.xml使用spring推荐的jackson-dataformat-xml库。 It will do automatic XML transformation for you as soon as JAXB library (which is inbuilt in JDK >= 1.6) is present, even without XML annotations. 存在JAXB库(在JDK> = 1.6中内置)后,即使没有XML批注,它也会为您自动进行XML转换。 However, you can use @JacksonXml.. annotations to given desired structure to your xml. 但是,您可以使用@JacksonXml..批注为xml提供所需的结构。

To achieve the desired result here, I will create a wrapper class and update my controller as below: 为了在这里达到预期的效果,我将创建一个包装器类并更新控制器,如下所示:

//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());
}

NOTE: XML Wrapper class is not required here. 注意:这里不需要XML Wrapper类。 Spring will just do fine with the default array transformation using <List><Items> , but its recommended to shape your XML as per desired structure. Spring可以很好地使用<List><Items>进行默认的数组转换,但是建议按照所需的结构对XML进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM