24
votes

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?

2
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... - inf3rno
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? - Tony Arad Felik
I'll check, maybe I have an imperfect memory of the HAL specification. - inf3rno
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. - inf3rno
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... - inf3rno

2 Answers

12
votes

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

1
votes

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>