spring-projects / spring-hateoas

Spring HATEOAS - Library to support implementing representations for hyper-text driven REST web services.
https://spring.io/projects/spring-hateoas
Apache License 2.0
1.04k stars 476 forks source link

Access HAL+JSON-_compatible_ resources that have a custom media type using Traverson #1050

Open mkhl opened 5 years ago

mkhl commented 5 years ago

I'm using Spring 5.1.8, Spring Boot 2.1.6, and Spring HATEOAS 0.25.1.

As an example, I want to access Spring Boot Actuator resources, which are transmitted with the media type application/vnd.spring-boot.actuator.v2+json.

For that to work, I have to provide a LinkDiscoverer for that media type:

    @Bean
    LinkDiscoverer actuatorLinkDiscoverer() {
        var halJsonPathTemplate = "$._links..['%s']..href"; // stolen from HalLinkDiscoverer
        return new JsonPathLinkDiscoverer(halJsonPathTemplate, parseMediaType(ActuatorMediaType.V2_JSON));
    }

The definition is basically a copy of HalLinkDiscoverer since I cannot create instances of that with a custom media type, or access the JSON Path template.

It would be nice if creating LinkDiscoverers for HAL+JSON-compatible media types were easier.

gregturn commented 5 years ago

Since new efforts target Spring Boot 2.2, I’ve created #1052 to focus on smoothing the connection between these two.

I need to review Spring Boot’s usage of hypermedia to ensure they are doing things properly as well so the whole experience makes sense.

mkhl commented 5 years ago

That's great to hear!

Since Actuator is just an example, I assume the way to support other media types is as described in https://docs.spring.io/spring-hateoas/docs/1.0.0.RC1/reference/html/#mediatypes.custom, and possibly by extending HalMediaTypeConfiguration if the type is compatible with HAL?