0
votes

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?

1
"Is there a way of using the spring-bundled HAL browser without using spring-data-rest" Yes. It's not from Spring. Spring Data Rest just includes it. But I think you misunderstood its purpose. As the name implies it allows you to browse, not to provide links.a better oliver
I get that (May the original description is unclear). The question is why spring-data-rest is not reusing the links already defined, and forces me to override the process method.runeaen
Can you elaborate on "re-use the links already defined with hateoas"? I also don't understand what you mean by _"Exposes the resource (controller) to the HAL browser". You don't create links only for the HAL browser.a better oliver
First thing first. 1) I have a resource. The resource have links (created with spring-hateoas classes). So far so good. However, the HAL browser bundled with spring-data-rest does not pick up on those links. I thought the spring-data-rest bundled hal browser would use these links. It doesnt.runeaen
2) I can remove this comment, it's an initial comment from one of the developers and I can see the confusionruneaen

1 Answers

2
votes

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).