Skip to content

Commit 99573ad

Browse files
author
eugenp
committed
marshalling work
1 parent 82fe27e commit 99573ad

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

spring-rest/src/main/java/org/baeldung/web/controller/FooController.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@ public FooController() {
2323
// API - read
2424

2525
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}")
26-
public @ResponseBody
27-
Foo findById(@PathVariable final long id) {
26+
@ResponseBody
27+
public Foo findById(@PathVariable final long id) {
2828
return new Foo(Long.parseLong(randomNumeric(2)), randomAlphabetic(4));
2929
}
3030

3131
// API - write
3232

3333
@RequestMapping(method = RequestMethod.PUT, value = "/foos/{id}")
3434
@ResponseStatus(HttpStatus.OK)
35-
public void updateFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) {
35+
@ResponseBody
36+
public Foo updateFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) {
3637
System.out.println(foo);
38+
return foo;
3739
}
38-
3940
}

spring-rest/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersIntegrationTestsCase.java

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.baeldung.web.test;
22

3+
import static org.hamcrest.Matchers.notNullValue;
4+
import static org.junit.Assert.assertThat;
5+
36
import java.util.ArrayList;
47
import java.util.Arrays;
58
import java.util.List;
@@ -13,6 +16,7 @@
1316
import org.springframework.http.MediaType;
1417
import org.springframework.http.ResponseEntity;
1518
import org.springframework.http.converter.HttpMessageConverter;
19+
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
1620
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
1721
import org.springframework.oxm.xstream.XStreamMarshaller;
1822
import org.springframework.web.client.RestTemplate;
@@ -29,20 +33,17 @@ public class SpringHttpMessageConvertersIntegrationTestsCase {
2933
* server (in this case json)
3034
*/
3135
@Test
32-
public void testGetFoo() {
36+
public void whenRetrievingAFoo_thenCorrect() {
3337
final String URI = BASE_URI + "foos/{id}";
3438

3539
final RestTemplate restTemplate = new RestTemplate();
3640
final Foo resource = restTemplate.getForObject(URI, Foo.class, "1");
3741

38-
Assert.assertEquals(1l, resource.getId());
42+
assertThat(resource, notNullValue());
3943
}
4044

41-
/**
42-
* Specifying Accept Header with application/xml for getting the xml response from the server.
43-
*/
4445
@Test
45-
public void testGetFooAcceptXML() {
46+
public void givenConsumingXml_whenReadingTheFoo_thenCorrect() {
4647
final String URI = BASE_URI + "foos/{id}";
4748

4849
final RestTemplate restTemplate = new RestTemplate();
@@ -55,22 +56,35 @@ public void testGetFooAcceptXML() {
5556
final ResponseEntity<Foo> response = restTemplate.exchange(URI, HttpMethod.GET, entity, Foo.class, "1");
5657
final Foo resource = response.getBody();
5758

58-
Assert.assertEquals(1l, resource.getId());
59+
assertThat(resource, notNullValue());
60+
}
61+
62+
@Test
63+
public void givenConsumingJson_whenReadingTheFoo_thenCorrect() {
64+
final String URI = BASE_URI + "foos/{id}";
65+
66+
final RestTemplate restTemplate = new RestTemplate();
67+
restTemplate.setMessageConverters(getMessageConverters());
68+
69+
final HttpHeaders headers = new HttpHeaders();
70+
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
71+
final HttpEntity<String> entity = new HttpEntity<String>(headers);
72+
73+
final ResponseEntity<Foo> response = restTemplate.exchange(URI, HttpMethod.GET, entity, Foo.class, "1");
74+
final Foo resource = response.getBody();
5975

76+
assertThat(resource, notNullValue());
6077
}
6178

62-
/**
63-
* Specifying Accept Header with application/xml for getting the xml response from the server.
64-
*/
6579
@Test
66-
public void testPUTFooXML() {
80+
public void givenConsumingXml_whenWritingTheFoo_thenCorrect() {
6781
final String URI = BASE_URI + "foos/{id}";
6882
final RestTemplate restTemplate = new RestTemplate();
6983
restTemplate.setMessageConverters(getMessageConverters());
7084

71-
final Foo resource = new Foo(4, "andres");
85+
final Foo resource = new Foo(4, "jason");
7286
final HttpHeaders headers = new HttpHeaders();
73-
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
87+
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
7488
headers.setContentType((MediaType.APPLICATION_XML));
7589
final HttpEntity<Foo> entity = new HttpEntity<Foo>(resource, headers);
7690

@@ -82,17 +96,16 @@ public void testPUTFooXML() {
8296

8397
// UTIL
8498

85-
/**
86-
* Configures Message Converters.
87-
*/
8899
private List<HttpMessageConverter<?>> getMessageConverters() {
89100
final List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
90-
// adds XML converter using XStreamMarshaller
91-
final XStreamMarshaller marshaller = new XStreamMarshaller();
92-
marshaller.setAnnotatedClasses(Foo.class);
93101

94-
final MarshallingHttpMessageConverter marshallingConverter = new MarshallingHttpMessageConverter(marshaller);
95-
converters.add(marshallingConverter);
102+
final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
103+
final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
104+
xmlConverter.setMarshaller(xstreamMarshaller);
105+
xmlConverter.setUnmarshaller(xstreamMarshaller);
106+
107+
converters.add(xmlConverter);
108+
converters.add(new MappingJackson2HttpMessageConverter());
96109

97110
return converters;
98111
}

0 commit comments

Comments
 (0)