简体   繁体   English

Java:编译冲突:返回类型与HATEOAS ResourceSupport.getId不兼容

[英]Java: compile conflict: return type is incompatible with HATEOAS ResourceSupport.getId

So I have this HATEOAS entity. 所以我有这个HATEOAS实体。

@Entity
@Table(name="users")
public class User extends ResourceSupport {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private long id;

    public User() {

    }

    public Long getId() {
    return new Long(id);
    }

    public void setId(Long id) {
    this.id = id.longValue();
    }
}  

My entity has an id of type long, but HATEOAS's ResourceSupport requires that getId return a Link. 我的实体的id类型为long,但HATEOAS的ResourceSupport要求getId返回一个Link。

The entity has a Long id because the db has a long id, and it is a persisted entity. 实体具有Long id,因为db具有long id,并且它是持久化实体。 How can I implement this entity with HATEOAS? 如何使用HATEOAS实现此实体?

Check out the "Link Builder" section of the documentation: 查看文档的“链接构建器”部分:

http://docs.spring.io/spring-hateoas/docs/current/reference/html/#fundamentals.obtaining-links.builder http://docs.spring.io/spring-hateoas/docs/current/reference/html/#fundamentals.obtaining-links.builder

There, it describes how to use a ControllerLinkBuilder to create the Link using a separate controller class. 在那里,它描述了如何使用ControllerLinkBuilder使用单独的控制器类创建Link Your User Object would implement Identifiable<Long> , as the example in the page above shows. 您的User对象将实现Identifiable<Long> ,如上页中的示例所示。

You can create one BeanResource bean which extends ResourceSupport bean like. 您可以创建一个扩展ResourceSupport bean的BeanResource bean。

@JsonIgnoreProperties({ "id" })
public class BeanResource extends ResourceSupport {

 @JsonUnwrapped
 private Object resorce;

 public Resource(Object resorce) {
    this.resorce = resorce;
 }

 public Object getResorce() {
    return resorce;
 }

}

just Unwrap resource instance property so that BeanResource bean will render json like user bean along with ResourceSupport bean will render link json object, 只需展开资源实例属性,以便BeanResource bean将像用户bean一样呈现json,以及ResourceSupport bean将呈现链接json对象,

after that you can create assembler like this. 之后你可以像这样创建汇编程序。

public class UserAssembler extends ResourceAssemblerSupport<User, BeanResource> {

public UserAssembler() {
    super(User.class, BeanResource.class);
}

@Override
public Resource toResource(User user) {
    Resource userResource = new Resource(user);
    try {
        Link selfLink = linkTo(
                methodOn(UserController.class).getUser(user.getId()))
                .withSelfRel();
        userResource.add(selfLink);

    } catch (EntityDoseNotExistException e) {
        e.printStackTrace();
    }
    return userResource;
}

}

and in controller just attach Resource bean which contains user bean like 并在控制器中只附加包含用户bean的Resource bean

@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
public ResponseEntity<Resource> getUser(@PathVariable String userId)
        throws EntityDoseNotExistException {
    User user = userService.getUser(userId);
    Resource userResource = userAssembler.toResource(user);
    return new ResponseEntity<Resource>(userResource, HttpStatus.OK);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM