Search code examples
spring-data-restspring-hateoashal-json

Using HAL-browser with spring-hateoas WITHOUT spring-data-rest


We're starting a new project, and we're looking into using spring-hateoas / hypermedia. The HAL browser also looks interesting, so we wanted to check it out.

However, the HAL browser seems bundled to spring-data-rest, which we don't want to use (for different reasons).

At a minimum we don't want to auto-expose all repositories as rest-resources, but when doing this we need to explicity define the links for the HAL browser in addition to defining the links with spring-hateoas.

When NOT auto-exposing the repositories, we have to define

implements ResourceProcessor<RepositoryLinksResource>

and

@Override
    RepositoryLinksResource process(RepositoryLinksResource resource) {
        resource.add(link('/{id}').withRel('my-dummy'))

        return resource
    }

in addition to already defined links (using hateoas)

link('/{id}').expand(entity.id).withSelfRel()

This seems cumbersome, and doesnt comply with the DRY principle. We would rather not implement the @Override method RepositoryLinksResource process(RepositoryLinksResource resource) as these links are already defined elsewhere.

My initial thought was that the HAL browser would re-use the links already defined with hateoas. But apparanlty I did not understand it correctly?

So the question is

Is there a way of using the spring-bundled HAL browser without using spring-data-rest?

And without having to manually define the links explicitly for the HAL browser when not auto-exposing the repositories?


Solution

  • The HAL browser provided by Spring can not be used without spring-data-rest.

    I had a similar need and had considered the following options...

    1. Package the Spring HAL browser in a separate project (hosted on a different port) and mark your original project with @CrossOrigin.
    2. Package the generic HAL browser.

    I did not attempt the first approach and so don't know if you would face any other issues with it.

    The second approach was done by downloading the HAL browser (https://github.com/mikekelly/hal-browser) and adding it to the Spring Boot project folder src/resources/public (https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot).