From: Gustavo Martin Morcuende Date: Sun, 3 Jan 2016 21:12:33 +0000 (+0100) Subject: Spring REST: Jackson requires default constructor X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=d9dd2c99f3d57c75c06a00e36ae9f5ac2c23270b;p=SpringWebServicesForFun%2F.git Spring REST: Jackson requires default constructor --- diff --git a/SpringJava/REST/src/main/java/de/spring/webservices/rest/Car.java b/SpringJava/REST/src/main/java/de/spring/webservices/rest/Car.java index 2dafc8d..4310f9d 100644 --- a/SpringJava/REST/src/main/java/de/spring/webservices/rest/Car.java +++ b/SpringJava/REST/src/main/java/de/spring/webservices/rest/Car.java @@ -5,6 +5,12 @@ public class Car { private final Long id; private final String content; + // Required by Jackson :/ + public Car() { + this.id = null; + this.content = null; + } + public Car(Long id, String content) { this.id = id; this.content = content; diff --git a/SpringJava/REST/src/main/resources/spring-configuration/mvc/rest/rest-config.xml b/SpringJava/REST/src/main/resources/spring-configuration/mvc/rest/rest-config.xml index 5f706e6..77232ab 100644 --- a/SpringJava/REST/src/main/resources/spring-configuration/mvc/rest/rest-config.xml +++ b/SpringJava/REST/src/main/resources/spring-configuration/mvc/rest/rest-config.xml @@ -9,6 +9,15 @@ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> + + + @@ -40,6 +49,15 @@ + + + + + + + + + diff --git a/SpringJava/REST/src/test/java/de/spring/webservices/rest/CarControllerIntegrationTest.java b/SpringJava/REST/src/test/java/de/spring/webservices/rest/CarControllerIntegrationTest.java index 3ed613c..090d2c4 100644 --- a/SpringJava/REST/src/test/java/de/spring/webservices/rest/CarControllerIntegrationTest.java +++ b/SpringJava/REST/src/test/java/de/spring/webservices/rest/CarControllerIntegrationTest.java @@ -38,7 +38,7 @@ public class CarControllerIntegrationTest { @Test public void testWhenGetAllCarsThenRetrieveJsonValues() throws Exception { mockMvc.perform(get("/api/cars/") - .accept(MediaType.APPLICATION_JSON_UTF8_VALUE)) + .accept(MediaType.APPLICATION_JSON_UTF8)) .andExpect(status().isOk()) .andExpect(jsonPath("$[0].id", any(Integer.class))) @@ -47,33 +47,33 @@ public class CarControllerIntegrationTest { .andExpect(jsonPath("$[1].id", any(Integer.class))) .andExpect(jsonPath("$[2].content", is("Car: 3"))) .andExpect(jsonPath("$[2].id", any(Integer.class))) - .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)); + .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)); } @Test public void testWhenGetOneCarThenRetrieveJsonValue() throws Exception { mockMvc.perform(get("/api/cars/{id}", 1L) - .accept(MediaType.APPLICATION_JSON_UTF8_VALUE)) + .accept(MediaType.APPLICATION_JSON_UTF8)) .andExpect(status().isOk()) .andExpect(jsonPath("id", any(Integer.class))) .andExpect(jsonPath("content", is("Car: 1"))) - .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)); + .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)); } @Test public void testWhenCreateNewCarThenRetrieveJsonValue() throws Exception { Car car = new Car(2L, "nothing"); mockMvc.perform(post("/api/cars/") - .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE) + .contentType(MediaType.APPLICATION_JSON_UTF8) .content(asJsonString(car)) - .accept(MediaType.APPLICATION_JSON_UTF8_VALUE)) + .accept(MediaType.APPLICATION_JSON_UTF8)) .andExpect(status().isCreated()) .andExpect(jsonPath("id", any(Integer.class))) .andExpect(jsonPath("content", is("Car: 1"))) .andExpect(header().string(HttpHeaders.LOCATION, "/api/cars/1")) - .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)); + .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)); } private static String asJsonString(final Object obj) throws JsonProcessingException {