From 1f92360583db74e0cf675d0b7afbc475932aaf4b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 30 Oct 2015 09:17:34 +0000 Subject: [PATCH] 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 --- .../java/sample/web/CustomerController.java | 5 ++-- .../sample/SampleHateoasApplicationTests.java | 23 ++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/spring-boot-samples/spring-boot-sample-hateoas/src/main/java/sample/web/CustomerController.java b/spring-boot-samples/spring-boot-sample-hateoas/src/main/java/sample/web/CustomerController.java index d7019428a8d..45cc3d0d52e 100644 --- a/spring-boot-samples/spring-boot-sample-hateoas/src/main/java/sample/web/CustomerController.java +++ b/spring-boot-samples/spring-boot-sample-hateoas/src/main/java/sample/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/SampleHateoasApplicationTests.java b/spring-boot-samples/spring-boot-sample-hateoas/src/test/java/sample/SampleHateoasApplicationTests.java index 365c60acc5a..94173c2a456 100644 --- a/spring-boot-samples/spring-boot-sample-hateoas/src/test/java/sample/SampleHateoasApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-hateoas/src/test/java/sample/SampleHateoasApplicationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ package sample; +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.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; +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 org.springframework.test.context.web.WebAppConfiguration; @@ -38,7 +43,6 @@ import static org.junit.Assert.assertThat; @SpringApplicationConfiguration(classes = SampleHateoasApplication.class) @WebAppConfiguration @IntegrationTest("server.port:0") -@DirtiesContext public class SampleHateoasApplicationTests { @Value("${local.server.port}") @@ -54,4 +58,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"))); + } + }