marshalling work
This commit is contained in:
parent
82fe27e433
commit
99573ad02b
|
@ -23,8 +23,8 @@ public class FooController {
|
|||
// API - read
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}")
|
||||
public @ResponseBody
|
||||
Foo findById(@PathVariable final long id) {
|
||||
@ResponseBody
|
||||
public Foo findById(@PathVariable final long id) {
|
||||
return new Foo(Long.parseLong(randomNumeric(2)), randomAlphabetic(4));
|
||||
}
|
||||
|
||||
|
@ -32,8 +32,9 @@ public class FooController {
|
|||
|
||||
@RequestMapping(method = RequestMethod.PUT, value = "/foos/{id}")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public void updateFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) {
|
||||
@ResponseBody
|
||||
public Foo updateFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) {
|
||||
System.out.println(foo);
|
||||
return foo;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package org.baeldung.web.test;
|
||||
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
@ -13,6 +16,7 @@ import org.springframework.http.HttpMethod;
|
|||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
|
||||
import org.springframework.oxm.xstream.XStreamMarshaller;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
@ -29,20 +33,17 @@ public class SpringHttpMessageConvertersIntegrationTestsCase {
|
|||
* server (in this case json)
|
||||
*/
|
||||
@Test
|
||||
public void testGetFoo() {
|
||||
public void whenRetrievingAFoo_thenCorrect() {
|
||||
final String URI = BASE_URI + "foos/{id}";
|
||||
|
||||
final RestTemplate restTemplate = new RestTemplate();
|
||||
final Foo resource = restTemplate.getForObject(URI, Foo.class, "1");
|
||||
|
||||
Assert.assertEquals(1l, resource.getId());
|
||||
assertThat(resource, notNullValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifying Accept Header with application/xml for getting the xml response from the server.
|
||||
*/
|
||||
@Test
|
||||
public void testGetFooAcceptXML() {
|
||||
public void givenConsumingXml_whenReadingTheFoo_thenCorrect() {
|
||||
final String URI = BASE_URI + "foos/{id}";
|
||||
|
||||
final RestTemplate restTemplate = new RestTemplate();
|
||||
|
@ -55,22 +56,35 @@ public class SpringHttpMessageConvertersIntegrationTestsCase {
|
|||
final ResponseEntity<Foo> response = restTemplate.exchange(URI, HttpMethod.GET, entity, Foo.class, "1");
|
||||
final Foo resource = response.getBody();
|
||||
|
||||
Assert.assertEquals(1l, resource.getId());
|
||||
|
||||
assertThat(resource, notNullValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifying Accept Header with application/xml for getting the xml response from the server.
|
||||
*/
|
||||
@Test
|
||||
public void testPUTFooXML() {
|
||||
public void givenConsumingJson_whenReadingTheFoo_thenCorrect() {
|
||||
final String URI = BASE_URI + "foos/{id}";
|
||||
|
||||
final RestTemplate restTemplate = new RestTemplate();
|
||||
restTemplate.setMessageConverters(getMessageConverters());
|
||||
|
||||
final HttpHeaders headers = new HttpHeaders();
|
||||
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
|
||||
final HttpEntity<String> entity = new HttpEntity<String>(headers);
|
||||
|
||||
final ResponseEntity<Foo> response = restTemplate.exchange(URI, HttpMethod.GET, entity, Foo.class, "1");
|
||||
final Foo resource = response.getBody();
|
||||
|
||||
assertThat(resource, notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConsumingXml_whenWritingTheFoo_thenCorrect() {
|
||||
final String URI = BASE_URI + "foos/{id}";
|
||||
final RestTemplate restTemplate = new RestTemplate();
|
||||
restTemplate.setMessageConverters(getMessageConverters());
|
||||
|
||||
final Foo resource = new Foo(4, "andres");
|
||||
final Foo resource = new Foo(4, "jason");
|
||||
final HttpHeaders headers = new HttpHeaders();
|
||||
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
|
||||
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
|
||||
headers.setContentType((MediaType.APPLICATION_XML));
|
||||
final HttpEntity<Foo> entity = new HttpEntity<Foo>(resource, headers);
|
||||
|
||||
|
@ -82,17 +96,16 @@ public class SpringHttpMessageConvertersIntegrationTestsCase {
|
|||
|
||||
// UTIL
|
||||
|
||||
/**
|
||||
* Configures Message Converters.
|
||||
*/
|
||||
private List<HttpMessageConverter<?>> getMessageConverters() {
|
||||
final List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
// adds XML converter using XStreamMarshaller
|
||||
final XStreamMarshaller marshaller = new XStreamMarshaller();
|
||||
marshaller.setAnnotatedClasses(Foo.class);
|
||||
|
||||
final MarshallingHttpMessageConverter marshallingConverter = new MarshallingHttpMessageConverter(marshaller);
|
||||
converters.add(marshallingConverter);
|
||||
final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
|
||||
final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
|
||||
xmlConverter.setMarshaller(xstreamMarshaller);
|
||||
xmlConverter.setUnmarshaller(xstreamMarshaller);
|
||||
|
||||
converters.add(xmlConverter);
|
||||
converters.add(new MappingJackson2HttpMessageConverter());
|
||||
|
||||
return converters;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue