gpt4 book ai didi

java - Spring Boot 示例 hatoas 不工作

转载 作者:行者123 更新时间:2023-11-30 03:14:18 25 4
gpt4 key购买 nike

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

当访问示例提供的客户服务时,我得到一个 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; 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.JAXBException: class sample.domain.Customer nor any of its super class is known to this context.]

这是由以下代码引起的:

    @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> {

如果可能的话,我想避免这种情况。

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

提前致谢!

最佳答案

该示例旨在使用 JSON,并且默认情况下将提供 JSON。我猜您正在发送带有请求 application/xmlAccept header 的请求。尝试删除 header ,或改为请求 application/json

关于java - Spring Boot 示例 hatoas 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32994792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com