简体   繁体   English

春季靴样本波音符号不起作用

[英]spring boot sample hateoas not working

I'm trying to run the hateoas spring boot example located at https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-hateoas (I'm trying to run Version 1.2.6.RELEASE). 我正在尝试运行位于https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-hateoas的hateoas春季启动示例(我是尝试运行1.2.6.RELEASE版本)。

When accessing the customers service provided by the example I get a MarshalException: 当访问该示例提供的客户服务时,我收到MarshalException:

Could not marshal [Resources { content: [sample.domain.Customer@72c01d5a, sample.domain.Customer@5ef1ebf9, sample.domain.Customer@237de11a], links: [ http://localhost:8080/customers ;rel="self"] }]: null; 无法编组[资源{内容:[sample.domain.Customer @ 72c01d5a,sample.domain.Customer @ 5ef1ebf9,sample.domain.Customer @ 237de11a],链接:[ http:// localhost:8080 / customers ; rel =“ self“]}]:null; nested exception is javax.xml.bind.MarshalException - with linked exception: [com.sun.istack.internal.SAXException2: class sample.domain.Customer nor any of its super class is known to this context. 嵌套的异常是javax.xml.bind.MarshalException-带有链接的异常:[com.sun.istack.internal.SAXException2:类sample.domain.Customer或其任何超类对此上下文都是已知的。 javax.xml.bind.JAXBException: class sample.domain.Customer nor any of its super class is known to this context.] javax.xml.bind.JAXBException:类sample.domain.Customer或其任何超类对此上下文都是已知的。

This is caused by the following code: 这是由以下代码引起的:

    @RequestMapping(method = RequestMethod.GET)
HttpEntity<Resources<Customer>> showCustomers() {
    Resources<Customer> resources = new Resources<Customer>(this.repository.findAll());
    resources.add(this.entityLinks.linkToCollectionResource(Customer.class));
    return new ResponseEntity<Resources<Customer>>(resources, HttpStatus.OK);
}

While I could probably implement my own 虽然我可能可以实现自己的

public class CustomerResources extends Resources<Customer> {

I'd like to avoid this if at all possible. 我想尽可能避免这种情况。

Also if it has to be like this I'm confused why it's not like this in the example. 另外,如果必须这样,我很困惑为什么在示例中它不是这样。 Am I missing something? 我想念什么吗? How can I get the sample to work? 我怎样才能得到样品?

Thanks in advance! 提前致谢!

The sample is intended to work with JSON and will serve JSON by default. 该示例旨在用于JSON,并且默认情况下将提供JSON。 I guess you're sending a request with an Accept header that requests application/xml . 我猜您正在发送带有Accept标头的请求,该请求请求application/xml Try removing the header, or requesting application/json instead. 尝试删除标题,或请求application/json代替。

While working with HATEOAS, Exception: javax.xml.bind.MarshalException occurs when you do not add correct producer type with URI. 在使用HATEOAS时,异常:当您未使用URI添加正确的生产者类型时,将发生javax.xml.bind.MarshalException

Whenever you use HATEOAS in Spring boot, you need to add produces type as application/hal+json . 每当您在Spring Boot中使用HATEOAS时,都需要将Produces类型添加为application / hal + json

Example: @PostMapping(value="/yourUri", produces = "application/hal+json" ) 示例:@PostMapping(value =“ / yourUri”, 产生=“ application / hal + json”

This worked for me! 这对我有用!

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

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