1

I´m using Spring Boot and HATEOAS to build a REST API and I am struggling with the curie creation. The Spring HATEOAS guide says that in order to automatically insert a curie in the responses, you should do the following:

@Configuration
@EnableWebMvc
@EnableHypermediaSupport(type= {HypermediaType.HAL})
public class Config {

  @Bean
  public CurieProvider curieProvider() {
    return new DefaultCurieProvider("ex", new UriTemplate("http://www.example.com{#rel}"));
  }
}

My config class is like this:

@SpringBootApplication
public class ApiApplication {

  public static void main(String[] args) {
    SpringApplication.run(ApiApplication.class, args);
  }

  @Bean
  public CurieProvider curieProvider() {
    return new DefaultCurieProvider("xpto", new UriTemplate("http://www.xpto.com{#rel}"));
  }
}

I tried to add the @EnableWebMvc to my config class but it changes the rendering of the response (hal) and the curie doesn´t appear. I have to do something on the controller to create the curie?

Update: I updated the Spring Hateoas (to 0.17.0.RELEASE) and now my collection name is rendered with the curie but the curie does not appear in the _links section:

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/technologies"
        }
    },
    "_embedded": {
        "mycurie:technology": [
            {
                "id": 1,
                "description": "A",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/technologies/1"
                    }
                }
            },
            {
                "id": 2,
                "description": "B",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/technologies/2"
                    }
                }
            }
        ]
    }
}

If I add one link to the _links section then the curie link appears:

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/technologies"
        },
        "mycurie:xpto": {
            "href": "http://localhost:8080/xpto"
        },
        "curies": [
            {
                "href": "http://localhost:8080/rels/{rel}",
                "name": "mycurie",
                "templated": true
            }
        ]
    },
    "_embedded": {
        "mycurie:technology": [
            {
                "id": 1,
                "description": "A",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/technologies/1"
                    }
                }
            },
            {
                "id": 2,
                "description": "B",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/technologies/2"
                    }
                }
            }
        ]
    }
}

This is my controller:

@RestController
@ExposesResourceFor(Technology.class)
@RequestMapping(value = "/technologies")
public class TechnologyRestController {

    ...

    @RequestMapping(method = RequestMethod.GET, produces = "application/vnd.xpto-technologies.text+json")
    public Resources<TechnologyResource> getAllTechnologies() {
        List<Technology> technologies = technologyGateway.getAllTechnologies();
        Resources<TechnologyResource> technologiesResources = new Resources<TechnologyResource>(technologyResourceAssembler.toResources(technologies));
        technologiesResources.add(linkTo(methodOn(TechnologyRestController.class).getAllTechnologies()).withSelfRel());
        return technologiesResources;
    }

}
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
jose_p
  • 624
  • 1
  • 8
  • 12

1 Answers1

0

Your config class looks correct to me, however Spring Boot's HypermediaAutoConfiguration requires org.springframework.plugin:spring-plugin-core, an optional dependency of Spring HATEOAS, to be on the classpath for it to be enabled. I'd guess that you're missing this dependency. Try adding a dependency on org.springframework.plugin:spring-plugin-core:1.1.0.RELEASE.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • I have that dependency on my pom. The only thing that doesn´t work is the curie. Aside from that, the representations returned by the API are rendered as expected (HAL). – jose_p Mar 03 '15 at 14:26