24

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?

Tony Arad Felik
  • 241
  • 1
  • 2
  • 3
  • 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 Sep 07 '14 at 13:00
  • Thanks. Do you know Hal Browser (http://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 Sep 07 '14 at 16:07
  • I'll check, maybe I have an imperfect memory of the HAL specification. – inf3rno Sep 07 '14 at 21:35
  • 1
    Yepp, you were right I guess I was in hurry, when I read your code. Your example has [collection+json](http://amundsen.com/media-types/collection/examples/) 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 Sep 07 '14 at 21:50
  • Read the manual here: https://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 Sep 07 '14 at 21:59
  • if the EnableHypermediaSupport didn't work for you, then you have something weird going on. Can you pose all your @Configuration and your Controller? – Chris DaMour Sep 16 '14 at 17:29
  • You need to include [Jackson 2 DataBind](https://github.com/FasterXML/jackson-databind) in your classpath. – jiwhiz Oct 30 '14 at 05:01

2 Answers2

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

inf3rno
  • 24,976
  • 11
  • 115
  • 197
  • 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 :-( – Tony Arad Felik Sep 08 '14 at 05:24
  • 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. – inf3rno Sep 08 '14 at 13:37
  • 1
    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. – Nathanael Apr 02 '15 at 19:23
  • That citation is now at https://www.jiwhiz.com/blogs/Design_and_Build_RESTful_API_with_Spring_HATEOAS – afaulconbridge Mar 14 '17 at 12:13
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>
afaulconbridge
  • 1,107
  • 9
  • 21
Naveen raj
  • 891
  • 1
  • 10
  • 18