[英]Migrate ResourceSupport to RepresentationModel
我有这段代码,我想迁移到最新的 Spring hatoas 版本。 我试过这个:
@JsonInclude(Include.NON_NULL)
public class TransactionResource extends RepresentationModel {
@JsonProperty("id")
private UUID uuid;
public void setUuid(UUID uuid) {
// Remove the hateoas ID (self referential link) if exists
if (getId() != null) {
getLinks().remove(getId());
}
add(linkTo(methodOn(TransactionController.class).lookup(uuid, null))
.withSelfRel()
.expand());
this.uuid = uuid;
}
......................
}
我收到错误无法解析'getId' in 'TransactionResource'
和Cannot resolve method 'remove' in 'Links'
你知道我必须如何解决这个问题吗?
我认为在较新的 Spring Hateoas 版本中,您可以使用以下代码获得类似的结果:
@JsonInclude(Include.NON_NULL)
public class TransactionResource extends RepresentationModel {
@JsonProperty("id")
private UUID uuid;
public void setUuid(UUID uuid) {
// Remove the hateoas ID (self referential link) if exists
if (this.hasLink(IanaLinkRelations.SELF)) {
this.getLinks().without(IanaLinkRelations.SELF);
}
add(linkTo(methodOn(TransactionController.class).lookup(uuid, null))
.withSelfRel()
.expand());
this.uuid = uuid;
}
......................
}
我还没有测试过,但可能上面的代码可以这样简化:
@JsonInclude(Include.NON_NULL)
public class TransactionResource extends RepresentationModel {
@JsonProperty("id")
private UUID uuid;
public void setUuid(UUID uuid) {
// Remove the hateoas ID (self referential link) if exists
this.getLinks().without(IanaLinkRelations.SELF);
add(linkTo(methodOn(TransactionController.class).lookup(uuid, null))
.withSelfRel()
.expand());
this.uuid = uuid;
}
......................
}
请注意,主要更改与RepresentationModel
和Links
中提供的方法的使用有关。
正如我所说,请注意我没有测试过代码。 我主要关心的是this.getLinks().without(IanaLinkRelations.SELF);
返回一个新的Links
实例,它可能不会替换与RepresentationModel
关联的现有实例,因此您可能需要merge
结果。 请考虑到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.