I have a Spring HATEOAS Resource
such that ModelResource extends Resource<Model>
.
In a @RestController
I have a method to create new Model
s:
@RequestMapping(value = "", method = POST)
public ResponseEntity<ModelResource> postModel() {
Model model = service.create().model;
ModelResource resource = assembler.toResource(model);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(URI.create(resource.getLink("self").getHref()));
return new ResponseEntity<>(resource, headers, CREATED);
}
The created ModelResource
returned from the above method is HAL-encoded:
$ curl -v -XPOST localhost:8080/models
> POST /models HTTP/1.1
> User-Agent: curl/7.32.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 201 Created
< Date: Sun, 25 Jan 2015 11:51:50 GMT
< Location: http://localhost:8080/models/0
< Content-Type: application/hal+json; charset=UTF-8
< Transfer-Encoding: chunked
< Server: Jetty(9.2.4.v20141103)
<
{
"id" : 0,
"_links" : {
"self" : {
"href" : "http://localhost:8080/models/0"
}
}
}
In the same controller also have a method to list Model
s.
@RequestMapping(value = "", method = GET)
public List<ModelResource> getModels() {
return service.find().stream()
.map(modelProxy -> assembler.toResource(modelProxy.model))
.collect(Collectors.toList());
}
For some reason, this method returns plain JSON, not HAL:
$ curl -v localhost:8080/models
> GET /models HTTP/1.1
> User-Agent: curl/7.32.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Sun, 25 Jan 2015 11:52:00 GMT
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Server: Jetty(9.2.4.v20141103)
<
[ {
"id" : 0,
"links" : [ {
"rel" : "self",
"href" : "http://localhost:8080/models/0"
} ]
} ]
- Why does the first method return HAL, and the second returns plain JSON?
- How can I specify consistent behavior?
I've read about @EnableHypermediaSupport
, but I don't have it set anywhere in my code.