【问题标题】:Java 11 + Spring Boot + HATEOAS + JAXBException: Class *** nor any of its super class is known to this contextJava 11 + Spring Boot + HATEOAS + JAXBException: Class *** 或其任何超类在此上下文中都是已知的
【发布时间】: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


    【解决方案1】:

    奇怪的是,这与浏览器有关。如果您使用“curl”之类的客户端而不是浏览器调用端点,它应该可以工作。 对我有帮助的解决方法 - 添加:

     , produces="application/json; charset=UTF-8"
    

    GetMapping()

    更多详情请访问: https://github.com/spring-guides/tut-rest/issues/64

    【讨论】:

      猜你喜欢
      • 2014-08-16
      • 2012-12-13
      • 2013-09-13
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多