Auto generate hal links for in spring boot

100
July 02, 2022, at 05:20 AM

Is it possible to have Spring Boot automatically generate hal links for rest controller just like it does for repository?

Lets assume that I have @Entity class named MyEntity.

If I create rest repository for this class, it automatically adds self link and all the other links to all kinds of related entities:

@RepositoryRestResource
public interface MyEntityRepository extends PagingAndSortingRepository<MyEntity, Long> {
  @Override
  Optional<MyEntity> findById(Long aLong);
}

Now if I want to return MyEntity from @RestController instead, is there way to tell Spring Boot that I want exactly the same links as it automatically generates me when I use repository?

Here is my rest service:

@RestController
@RequestMapping(value = "/api/some/path/MyEntitys", produces = HAL_JSON_VALUE)
public class MyEntityRestController {
  // ...
    
  @GetMapping("/{id}")
  public ResponseEntity<EntityModel<MyEntity>> findById(@PathVariable Long id) {
    Optional<MyEntity> myEntityOptional = myEntityService.findById(id);
    MyEntity MyEntity = myEntityOptional.get();
    return ResponseEntity.ok(EntityModel.of(MyEntity));
  }
}

When I visit repository endpoint /api/MyEntitys/123 directly I get response that has _links that looks something like:

"_links" : {
  "self" : {
    "href" : ...
  },
  "relatedEntity1" : {
    "href" : "...{?projection}",
    "templated" : true
  },
  "relatedEntity2" : {
    "href" : "...{?projection}",
    "templated" : true
  },
  ...
}

But when I visit /api/some/path/MyEntitys/123 then I dont have any links.

From here https://docs.spring.io/spring-hateoas/docs/current/reference/html/#server.processors I can learn about links.

I learn that I can do something like this:

@Component
public class MyEntityRepresentationModelProcessor implements RepresentationModelProcessor<EntityModel<MyEntity>> {
  @Override
  public EntityModel<MyEntity> process(EntityModel<MyEntity> em) {
    em.add(Link.of("..."));
    em.add(Link.of("...").withRel("relatedEntity1"));
    em.add(Link.of("...").withRel("relatedEntity2"));
    return em;
  }
}

to get similar results.

Question: is manually building and adding those links one by one the only way or does Spring Boot offer anything to automatically generate exactly the same links for rest controller as it generates for repository?

Rent Charter Buses Company
READ ALSO
How to read ArrayList&lt;String&gt; field from database with PostgreSQL JDBC

How to read ArrayList<String> field from database with PostgreSQL JDBC

I'm having an issue with getting the value of stored ArrayList<String> as VARCHAR[] in the database

97
Connection error returned from successful microservices call

Connection error returned from successful microservices call

Consider a scenario where I have 3 microservices say A, B, and C

129
Regex: Only returns message string - That&#39;s starts with messages and string between parent message curly brace

Regex: Only returns message string - That's starts with messages and string between parent message curly brace

I want to get all the message data onlySuch that it should look for message and all the data between curly braces of the parent message

123
Java 8 Mvn Project doesn&#39;t work on Java 17 [closed]

Java 8 Mvn Project doesn't work on Java 17 [closed]

Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problemThis will help others answer the question

72