0

I am using Spring Boot (2.1.1) to automatically create an HAL REST API of my JpaRepository interfaces.

For most cases these interfaces are empty, for example:

public interface LevelRepository extends JpaRepository<Level, Long> {}

When I open my REST base path, the following link is generated for levels:

"levels": {
  "href": "http://localhost:8080/admin/levels{?page,size,sort}",
  "templated": true
} 

When I follow http://localhost:8080/admin/levels?size=10 I get the expected page 0 and its 10 elements. But the given self link is:

"self": {
  "href": "http://localhost:8080/admin/levels{&sort}",
  "templated": true
}

I would have expected:

  • http://localhost:8080/admin/levels{?page,size,sort} or
  • http://localhost:8080/admin/levels?page=0&size=10{&sort}

I am not sure if this is a bug or a feature? Is it possible to get the expected behavior?

I have found the following question on the topic: Error on generating self link on pageable resource But the given solution does not help, as I am already using a newer version.

Further informations (Why do I need it?)

This behavior breaks the flow of following links on the client side. If the user wants to jump directly to page X or if he wants to change the page size, the original link must be reused. Not a big workaround, but it is not as nice as templating and following the given self link directly.

gillesB
  • 1,061
  • 1
  • 14
  • 30

2 Answers2

1

After posting this question I found the following Spring JIRA ticket describing the same behavior. According to a comment in the ticket the self link should not be templated.

Although the generated self link is templated, it is probably better to ignore the given parameters.

gillesB
  • 1,061
  • 1
  • 14
  • 30
0

You could re-implement the self link by yourself. First of all, I think you use HATEOAS library:

<dependency>
    <groupId>org.springframework.hateoas</groupId>
    <artifactId>spring-hateoas</artifactId>
</dependency>

Let's consider some of your entity - ExampleEntity:

public class ExampleEntity extends ResourceSupport {
    private String id;
    private String name;
    private String surname;

    // standard getters and setters
}

You extended the ResourceSupport class, and it has the link property, which we could use using add method:

ExampleEntity exampleEntity = new ExampleEntity();
Link selfLink = ControllerLinkBuilder.linkTo(YourController.class).slash(id).withSelfRel();
exampleEntity.add(selfLink);

So, you can change behaviour of your self link

BSeitkazin
  • 2,889
  • 25
  • 40