Java 11 + Spring Boot + HATEOAS + JAXBException: Class *** 它的任何超级 class 在此上下文中都不为人所知

Java 11 + Spring Boot + HATEOAS + JAXBException: Class *** nor any of its super class is known to this context

我正在尝试实现一个简单的服务并使用 spring-boot 中的 HATEOAS 资源来显示 link。当服务 运行 时,它会在控制台中抛出一条警告消息,内容如下:

javax.xml.bind.JAXBException: class com.in28minutes.rest.webservices.restfulwebservices.user.User 其任何超级 class 在此上下文中都不为人所知

我正在使用 JDK 11,这迫使我添加依赖项,因为我收到了 ClassNotFoundException: "org.glassfish.jaxb:jaxb-runtime"

但在添加该依赖项后,spring 资源 HATEOAS class 无法编组。

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