Spring REST: Jackson requires default constructor
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 3 Jan 2016 21:12:33 +0000 (22:12 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 3 Jan 2016 21:12:33 +0000 (22:12 +0100)
SpringJava/REST/src/main/java/de/spring/webservices/rest/Car.java
SpringJava/REST/src/main/resources/spring-configuration/mvc/rest/rest-config.xml
SpringJava/REST/src/test/java/de/spring/webservices/rest/CarControllerIntegrationTest.java

index 2dafc8d..4310f9d 100644 (file)
@@ -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;
index 55dd233..551f926 100644 (file)
@@ -9,6 +9,15 @@
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd\r
         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">\r
    \r
+       <!--\r
+               I am declaring my beans without the automatic annotation. :/\r
+               Better because we are saving memory but it requires more configuration.\r
+               \r
+               See: org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser\r
+               <mvc:annotation-driven/>\r
+        -->\r
+        \r
+   \r
        <context:annotation-config />\r
    \r
        <context:component-scan base-package="de.spring.webservices.rest"/>\r
                        </bean>\r
                </property>\r
                <property name="messageConverters" ref="messageConverters" />\r
+               \r
+               \r
+               <property name="requestBodyAdvice">\r
+                       <util:list>\r
+                               <bean id="requestBodyAdvice" class="org.springframework.web.servlet.mvc.method.annotation.JsonViewRequestBodyAdvice"/>\r
+                       </util:list>\r
+               </property>\r
+               \r
+               \r
                <property name="responseBodyAdvice">\r
                        <util:list>\r
                                <bean id="responseBodyAdvice" class="org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice"/>\r
index 3ed613c..090d2c4 100644 (file)
@@ -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 {