How to change HAL links format using Spring HATEOAS
Asked Answered
D

2

25

I'm building a Spring REST application using Spring HATEOAS (0.16.0.RELEASE) and I'd like the JSON links output to look like:

_links: {
   self: {
     href: "https://<ip>/api/policies/321"
   }
}

while it renders like:

   "links":
      [{
       "rel":"self",
       "href":"http://<ip>/api/policies/321"
      }]

I'm using HATEOAS Resource and ResourceAssembler.

Why do I get this format instead of the other? How can I change it?

Dowd answered 7/9, 2014 at 11:25 Comment(7)
I guess it depends on the serializer you use to build the HAL response. It is perfectly valid by HAL, so a good HAL parser can work with it...Henbit
Thanks. Do you know Hal Browser (haltalk.herokuapp.com/explorer/browser.html#)? It doesn't work with this format so I thought it might not be a valid, by-the-book HAL format. Am I wrong?Dowd
I'll check, maybe I have an imperfect memory of the HAL specification.Henbit
Yepp, you were right I guess I was in hurry, when I read your code. Your example has collection+json structure and not hal+json. So I guess you are using a wrong class to generate the json response. By hal, you use _links and an object where the key is the link relation and the value can be an array of links or a single link. By collection+json you use links which is an array of links.Henbit
Read the manual here: github.com/spring-projects/spring-hateoas , this is the plain JSON format of Spring HATEOAS. (It is just similar to collection+json, but at first glance I don't think it is.) I think you have to use a different class, or different settings if you want to build a HAL response. This information is somewhere in the manual, but it is too long for me, since I don't develop java, maybe android if I have time for another hobby...Henbit
if the EnableHypermediaSupport didn't work for you, then you have something weird going on. Can you pose all your @Configuration and your Controller?Bahena
You need to include Jackson 2 DataBind in your classpath.Roshan
H
12

In order to use HAL as the message format language for our RESTful API, and enable automatic pagination, we need some configuration changes in our applicaiton. Since Spring Data and Spring HATEOAS already provides annotations for configuration, all we need is to add those annotations:

@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
@EnableHypermediaSupport(type = { HypermediaType.HAL })
@ComponentScan(basePackages = {
        "com.jiwhiz.rest"
})
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer c) {
        c.defaultContentType(MediaTypes.HAL_JSON);
    }
}

@EnableSpringDataWebSupport will add support for pagination and @EnableHypermediaSupport(type = { HypermediaType.HAL }) will add hypermedia support. Then we set default content type to application/hal+json.

cite: Design and Build RESTful API with Spring HATEOAS by Yuan Ji

Henbit answered 7/9, 2014 at 22:11 Comment(4)
I see you've been investigating this... Thanks! The blog you mentioned is the one I’ve been following and my application is configured just as you wrote above. still, my response format is as I wrote :-(Dowd
Pff, then sorry pal, I cannot help, I have just a few months experience with java. Try to create an issue by the github page of spring hateoas, I think they can help.Henbit
I had the same problem. It took me a day to find out whats wrong. But when I instanciate the WebConfig as @Bean and not import the configuration then it works ... very strange.Sideline
That citation is now at jiwhiz.com/blogs/…Hermaphroditus
L
1

Make sure that your using com.fasterxml.jackson dependency instead of others like org.codehaus.jackson. For example, in a Maven pom.xml :

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.5.3</version>
        </dependency>
Lightship answered 5/5, 2015 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.