【发布时间】:2019-02-12 15:54:27
【问题描述】:
我正在尝试实现一个简单的服务并使用 spring-boot 中的 HATEOAS 资源来显示链接。当服务运行时,它会在控制台中抛出一条 WARN 消息,内容如下:
javax.xml.bind.JAXBException: class com.in28minutes.rest.webservices.restfulwebservices.user.User 及其任何超类对此上下文都是未知的
我正在使用 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;
}
【问题讨论】:
标签: java spring-boot jaxb spring-hateoas java-11