Ensure that HATEOAS sample does not try to produce XML

The HATEOAS sample does not support XML responses. Previously, the
controller doesn't constrain the media types that it could produce.
This would result in a failure when handling a request that prefers
XML responses.

This commit updates the produces clauses in the controller so that
the sample will only attempt to produce JSON.

Closes gh-4343
This commit is contained in:
Andy Wilkinson
2015-10-30 09:17:34 +00:00
parent 05b501ce41
commit 1f92360583
2 changed files with 23 additions and 5 deletions

View File

@@ -26,6 +26,7 @@ import org.springframework.hateoas.Resource;
import org.springframework.hateoas.Resources;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
@@ -47,7 +48,7 @@ public class CustomerController {
this.entityLinks = entityLinks;
}
@RequestMapping(method = RequestMethod.GET)
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
HttpEntity<Resources<Customer>> showCustomers() {
Resources<Customer> resources = new Resources<Customer>(
this.repository.findAll());
@@ -55,7 +56,7 @@ public class CustomerController {
return new ResponseEntity<Resources<Customer>>(resources, HttpStatus.OK);
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
HttpEntity<Resource<Customer>> showCustomer(@PathVariable Long id) {
Resource<Customer> resource = new Resource<Customer>(this.repository.findOne(id));
resource.add(this.entityLinks.linkToSingleResource(Customer.class, id));