简体   繁体   English

将 ResourceSupport 迁移到 RepresentationModel

[英]Migrate ResourceSupport to RepresentationModel

I have this code which I would like to migrate to latest Spring hateoas version.我有这段代码,我想迁移到最新的 Spring hatoas 版本。 I tried this:我试过这个:

@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;
  }
  ......................
}

I get error Cannot resolve method 'getId' in 'TransactionResource' and Cannot resolve method 'remove' in 'Links'我收到错误无法解析'getId' in 'TransactionResource'Cannot resolve method 'remove' in 'Links'

Do you know how I have to fix this issue?你知道我必须如何解决这个问题吗?

I think that in the newer Spring Hateoas version you could achieve a similar result with this code:我认为在较新的 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;
  }
  ......................
}

I haven't tested it but probably the above code could be simplified like this:我还没有测试过,但可能上面的代码可以这样简化:

@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;
  }
  ......................
}

Note that the main changes are related to the use of the methods provided in RepresentationModel and in the Links .请注意,主要更改与RepresentationModelLinks中提供的方法的使用有关。

As I told, please, be aware that I haven't tested the code.正如我所说,请注意我没有测试过代码。 My main concern has to do with the fact that this.getLinks().without(IanaLinkRelations.SELF);我主要关心的是this.getLinks().without(IanaLinkRelations.SELF); return a new Links instance and it may not replace in place the existing ones associated with the RepresentationModel , so you probably would need to merge the results.返回一个新的Links实例,它可能不会替换RepresentationModel关联的现有实例,因此您可能需要merge结果。 Please, take that into account.请考虑到这一点。

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

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