春天的靴子样本hateoas不工作

时间:2015-10-07 14:21:18

标签: java spring spring-mvc spring-boot

我试图运行位于https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-hateoas的hateoas spring boot示例(我试图运行版本1.2.6.RELEASE)。

当访问示例提供的客户服务时,我得到MarshalException:

  

无法编组[资源{内容:   [sample.domain.Customer@72c01d5a,sample.domain.Customer@5ef1ebf9,   sample.domain.Customer@237de11a],链接:   [http://localhost:8080/customers; rel =" self"]}]:null;嵌套   异常是javax.xml.bind.MarshalException - 带有链接异常:   [com.sun.istack.internal.SAXException2:class sample.domain.Customer   在这种情况下,也不知道它的任何超类。   javax.xml.bind.JAXBException:类sample.domain.Customer也不是   它的超类在这种情况下是已知的。]

这是由以下代码引起的:

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

虽然我可以实现自己的

public class CustomerResources extends Resources<Customer> {

我想尽可能避免这种情况。

此外,如果它必须像这样,我很困惑为什么它在例子中不是这样的。我错过了什么吗?我怎样才能让样本起作用?

提前致谢!

2 个答案:

答案 0 :(得分:2)

该示例旨在使用JSON,默认情况下将提供JSON。我猜您发送的请求带有Accept标头,请求application/xml。请尝试删除标题,或者请求application/json

答案 1 :(得分:0)

在使用HATEOAS时,异常: javax.xml.bind.MarshalException 发生在您未使用URI添加正确的生产者类型时。

每当您在Spring Boot中使用HATEOAS时,都需要将农产品类型添加为 application / hal + json

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

这对我有用!