我正在尝试实现一个简单的服务并使用 spring-boot 中的 HATEOAS 资源来显示链接。当服务运行时,它会在控制台中抛出一条警告消息,内容如下:
javax.xml.bind.JAXBException:此上下文已知类 com.in28minutes.rest.webservices.restfulwebservices.user.User 或其任何父类(super class)
我使用的是 JDK 11,这迫使我添加依赖项,因为我收到了 ClassNotFoundException: “org.glassfish.jaxb:jaxb-runtime”
但是添加该依赖项后,无法对 spring Resource HATEOAS 类进行编码。
public class User {
private Integer id;
@Size(min=2, message="The name should have at least 2 characters")
private String name;
@Past
private LocalDate birthDate;
public User() {
}
public User(Integer id, String name, LocalDate birthDate) {
super();
this.id = id;
this.name = name;
this.birthDate = birthDate;
}
...
}
@GetMapping("/users/{id}")
public Resource<User> retrieveUser(@PathVariable("id") int theId) {
User aUserResult = service.findOne(theId);
if (aUserResult == null) {
throw new UserNotFoundException("id-" + theId);
}
Resource<User> aUserResource = new Resource<User>(aUserResult);
ControllerLinkBuilder aLinkTo = linkTo(methodOn(this.getClass()).retrieveAllUsers());
aUserResource.add(aLinkTo.withRel("all-users"));
return aUserResource;
}
最佳答案
奇怪的是,这与浏览器有关。如果您使用“curl”等客户端而不是浏览器来调用端点,它应该可以工作。 对我有帮助的解决方法 - 添加:
, produces="application/json; charset=UTF-8"
到GetMapping()
更多详情请访问: https://github.com/spring-guides/tut-rest/issues/64
关于Java 11 + Spring Boot + HATEOAS + JAXBException : Class *** nor any of its super class is known to this context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54653976/