Skip to content

Commit 1f92360

Browse files
committed
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
1 parent 05b501c commit 1f92360

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

spring-boot-samples/spring-boot-sample-hateoas/src/main/java/sample/web/CustomerController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.hateoas.Resources;
2727
import org.springframework.http.HttpEntity;
2828
import org.springframework.http.HttpStatus;
29+
import org.springframework.http.MediaType;
2930
import org.springframework.http.ResponseEntity;
3031
import org.springframework.stereotype.Controller;
3132
import org.springframework.web.bind.annotation.PathVariable;
@@ -47,15 +48,15 @@ public CustomerController(CustomerRepository repository, EntityLinks entityLinks
4748
this.entityLinks = entityLinks;
4849
}
4950

50-
@RequestMapping(method = RequestMethod.GET)
51+
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
5152
HttpEntity<Resources<Customer>> showCustomers() {
5253
Resources<Customer> resources = new Resources<Customer>(
5354
this.repository.findAll());
5455
resources.add(this.entityLinks.linkToCollectionResource(Customer.class));
5556
return new ResponseEntity<Resources<Customer>>(resources, HttpStatus.OK);
5657
}
5758

58-
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
59+
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
5960
HttpEntity<Resource<Customer>> showCustomer(@PathVariable Long id) {
6061
Resource<Customer> resource = new Resource<Customer>(this.repository.findOne(id));
6162
resource.add(this.entityLinks.linkToSingleResource(Customer.class, id));

spring-boot-samples/spring-boot-sample-hateoas/src/test/java/sample/SampleHateoasApplicationTests.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,21 @@
1616

1717
package sample;
1818

19+
import java.net.URI;
20+
1921
import org.junit.Test;
2022
import org.junit.runner.RunWith;
2123

2224
import org.springframework.beans.factory.annotation.Value;
2325
import org.springframework.boot.test.IntegrationTest;
2426
import org.springframework.boot.test.SpringApplicationConfiguration;
2527
import org.springframework.boot.test.TestRestTemplate;
28+
import org.springframework.http.HttpHeaders;
29+
import org.springframework.http.HttpMethod;
2630
import org.springframework.http.HttpStatus;
31+
import org.springframework.http.MediaType;
32+
import org.springframework.http.RequestEntity;
2733
import org.springframework.http.ResponseEntity;
28-
import org.springframework.test.annotation.DirtiesContext;
2934
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3035
import org.springframework.test.context.web.WebAppConfiguration;
3136

@@ -38,7 +43,6 @@
3843
@SpringApplicationConfiguration(classes = SampleHateoasApplication.class)
3944
@WebAppConfiguration
4045
@IntegrationTest("server.port:0")
41-
@DirtiesContext
4246
public class SampleHateoasApplicationTests {
4347

4448
@Value("${local.server.port}")
@@ -54,4 +58,17 @@ public void hasHalLinks() throws Exception {
5458
assertThat(entity.getBody(), containsString("_links\":{\"self\":{\"href\""));
5559
}
5660

61+
@Test
62+
public void producesJsonWhenXmlIsPreferred() throws Exception {
63+
HttpHeaders headers = new HttpHeaders();
64+
headers.set(HttpHeaders.ACCEPT, "application/xml;q=0.9,application/json;q=0.8");
65+
RequestEntity<?> request = new RequestEntity<Void>(headers, HttpMethod.GET,
66+
URI.create("http://localhost:" + this.port + "/customers/1"));
67+
ResponseEntity<String> response = new TestRestTemplate().exchange(request,
68+
String.class);
69+
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
70+
assertThat(response.getHeaders().getContentType(),
71+
equalTo(MediaType.parseMediaType("application/json;charset=UTF-8")));
72+
}
73+
5774
}

0 commit comments

Comments
 (0)