Java 11 + Spring Boot + HATEOAS + JAXBException:类***或其任何超类对此上下文都是已知的

时间:2019-02-12 15:54:27

标签: java spring-boot jaxb spring-hateoas java-11

我试图实现一个简单的服务,并使用spring-boot中的HATEOAS资源来显示链接。服务运行时,它将在控制台中显示以下警告消息:

javax.xml.bind.JAXBException:类com.in28minutes.rest.webservices.restfulwebservices.user.User或其任何超类对此上下文都是已知的

我正在使用JDK 11,因为我收到了ClassNotFoundException,这迫使我添加了依赖关系: “ org.glassfish.jaxb:jaxb运行时”

但是在添加了这种依赖关系之后,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;
}

1 个答案:

答案 0 :(得分:1)

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

GetMapping()

import { Component, OnInit } from '@angular/core'; import { Storage } from '@ionic/storage'; import { GetService } from '../../get.service'; @Component({ selector: 'app-canjepuntos', templateUrl: './canjepuntos.page.html', styleUrls: ['./canjepuntos.page.scss'], }) export class CanjepuntosPage implements OnInit { points: any; puntoscanje: any = 0; //You need to initialize a value for puntoscanje descuento: any; pcanje: any; constructor(private storage: Storage,) { this.storage.get('Puntos').then((val) => { this.points = parseInt(val); //console.log('Your Name is', val); }); } myFunction(event) { event.preventDefault(); this.puntoscanje = Number(`${this.puntoscanje}${event.key}`) if (isNaN(this.puntoscanje)) { this.puntoscanje = 0; } if( this.puntoscanje >= this.points){ this.puntoscanje = this.points; } var PuntosDescto = 10 ; var calculo = ( this.puntoscanje / PuntosDescto).toFixed(2); this.descuento = calculo; } }

更多详细信息,请访问: https://github.com/spring-guides/tut-rest/issues/64