Search code examples
spring-datahateoasspring-hateoas

How do I export data in accordance to HAL spec?


According to this specification, applications exposing data in hal+json format should provide embedded links in the _links json field. However, if I declare my REST Repository in spring-data 1.5.9 like this:

@RepositoryRestResource(path = "storage-node", excerptProjection = IBrief.class)
public interface IStorageNodeRepository extends JpaRepository<StorageNode, Long> {
  @Query("FROM StorageNode sn WHERE sn.parent is null order by sn.uploadTs ASC")
  List<StorageNode> findAllRootNodes();
}

I get the following json:

{
  "nodeType" : "Image",
  "text" : "100.jpeg",
  "uploadTs" : "2018-01-11T23:48:25.724Z",
   /* ... Irrelevant ... */
  "links" : [ {
    "rel" : "self",
    "href" : "http://localhost:8080/emerald/api/hal/storage-node/31"
  }
  /* ... Irrelevant ... */
}

As we can see, it exports links in "links" field, without the leading underscore. Is there additional configuration required to make it export data in accordance to HAL spec?


Solution

  • After giving it some thought, I've discovered this fragment.

      @Bean
      public RepositoryRestConfigurer repositoryRestConfigurer() {
        return new RepositoryRestConfigurerAdapter() {
          @Override
          public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
            config.setBasePath("/api/hal");
            config.setDefaultMediaType(MediaType.APPLICATION_JSON_UTF8);
          }
        };
      }
    

    Removing default media type did the job. I don't remember exactly under which circumstances I've added this. Maybe I had to explicitly say ;charset=utf8 in each response to deal with russian strings.