diff --git a/spring-boot-samples/spring-boot-sample-hateoas/src/main/java/sample/hateoas/web/CustomerController.java b/spring-boot-samples/spring-boot-sample-hateoas/src/main/java/sample/hateoas/web/CustomerController.java index 8f794ede61..d944cb09ca 100644 --- a/spring-boot-samples/spring-boot-sample-hateoas/src/main/java/sample/hateoas/web/CustomerController.java +++ b/spring-boot-samples/spring-boot-sample-hateoas/src/main/java/sample/hateoas/web/CustomerController.java @@ -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> showCustomers() { Resources resources = new Resources( this.repository.findAll()); @@ -55,7 +56,7 @@ public class CustomerController { return new ResponseEntity>(resources, HttpStatus.OK); } - @RequestMapping(value = "/{id}", method = RequestMethod.GET) + @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) HttpEntity> showCustomer(@PathVariable Long id) { Resource resource = new Resource(this.repository.findOne(id)); resource.add(this.entityLinks.linkToSingleResource(Customer.class, id)); diff --git a/spring-boot-samples/spring-boot-sample-hateoas/src/test/java/sample/hateoas/SampleHateoasApplicationTests.java b/spring-boot-samples/spring-boot-sample-hateoas/src/test/java/sample/hateoas/SampleHateoasApplicationTests.java index f0ae3bc468..511024cfa4 100644 --- a/spring-boot-samples/spring-boot-sample-hateoas/src/test/java/sample/hateoas/SampleHateoasApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-hateoas/src/test/java/sample/hateoas/SampleHateoasApplicationTests.java @@ -16,6 +16,8 @@ package sample.hateoas; +import java.net.URI; + import org.junit.Test; import org.junit.runner.RunWith; @@ -23,9 +25,12 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; import org.springframework.boot.test.WebIntegrationTest; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.RequestEntity; import org.springframework.http.ResponseEntity; -import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.hamcrest.Matchers.containsString; @@ -36,7 +41,6 @@ import static org.junit.Assert.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(SampleHateoasApplication.class) @WebIntegrationTest(randomPort = true) -@DirtiesContext public class SampleHateoasApplicationTests { @Value("${local.server.port}") @@ -52,4 +56,17 @@ public class SampleHateoasApplicationTests { assertThat(entity.getBody(), containsString("_links\":{\"self\":{\"href\"")); } + @Test + public void producesJsonWhenXmlIsPreferred() throws Exception { + HttpHeaders headers = new HttpHeaders(); + headers.set(HttpHeaders.ACCEPT, "application/xml;q=0.9,application/json;q=0.8"); + RequestEntity request = new RequestEntity(headers, HttpMethod.GET, + URI.create("http://localhost:" + this.port + "/customers/1")); + ResponseEntity response = new TestRestTemplate().exchange(request, + String.class); + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + assertThat(response.getHeaders().getContentType(), + equalTo(MediaType.parseMediaType("application/json;charset=UTF-8"))); + } + }