Skip to content
Advertisement

Migrate builder to Spring hateoas 2.6.7

I have this old code implemented in hateoas:1.0

  public class StaticPathLinkBuilder extends LinkBuilderSupport<StaticPathLinkBuilder> {

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

I updated my code to hateoas 2.6.7 but the code is changed this way:

public class StaticPathLinkBuilder extends LinkBuilderSupport<StaticPathLinkBuilder> {

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

What is the proper way to implement this change? I tried this:

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

But it’s not clear how I have to implement the code that I can send affordances.

Can you advice what is the proper way to implement this?

Advertisement

Answer

As you can see in its source code LinkBuilderSupport already provides a constructor with the two required arguments, UriComponents and List<Affordance>.

In the own library codebase, different LinkBuilders implementations as BasicLinkBuilder or TemplateVariableAwareLinkBuilderSupport already takes advantage of this fact in their implementations.

In your use case, you could try something similar to this:

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);
  }
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement