将构建器迁移到 Spring hateoas 2.6.7

Migrate builder to Spring hateoas 2.6.7

我在 hateoas:1.0

中实现了这个旧代码
  public class StaticPathLinkBuilder extends LinkBuilderSupport<StaticPathLinkBuilder> {

  @Override
  protected StaticPathLinkBuilder createNewInstance(UriComponentsBuilder builder) {
    return new StaticPathLinkBuilder(builder);
  }

我将我的代码更新为 hateoas 2.6.7,但代码是这样更改的:

public class StaticPathLinkBuilder extends LinkBuilderSupport<StaticPathLinkBuilder> {

@Override
  protected StaticPathLinkBuilder createNewInstance(UriComponents components, List<Affordance> affordances) {
    return null;
  }

实施此更改的正确方法是什么?我试过这个:

  @Override
  protected StaticPathLinkBuilder createNewInstance(UriComponents components, List<Affordance> affordances) {
    return new StaticPathLinkBuilder(UriComponentsBuilder.newInstance().uriComponents(components));
  }

但我不清楚如何实现我可以发送的代码 affordances

你能建议什么是正确的实现方法吗?

如您在其源代码中所见,LinkBuilderSupport 已经提供了带有两个必需参数的 constructorUriComponentsList<Affordance>

在自己的库代码库中,不同的 LinkBuilder 实现如 BasicLinkBuilder or TemplateVariableAwareLinkBuilderSupport 已经在其实现中利用了这一事实。

在您的用例中,您可以尝试类似的操作:

public class StaticPathLinkBuilder extends LinkBuilderSupport<StaticPathLinkBuilder> {

  private StaticPathLinkBuilder(UriComponents components, List<Affordance> affordances) {
    super(components, affordances);
  }

  @Override
  protected StaticPathLinkBuilder createNewInstance(UriComponents components, List<Affordance> affordances) {
    return new StaticPathLinkBuilder(components, affordances);
  }
}