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>
Fri, 8 Jan 2016 21:46:16 +0000 (22:46 +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 5f706e6..77232ab 100644 (file)
@@ -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">
    
+       <!--
+               I am declaring my beans without the automatic annotation. :/
+               Better because we are saving memory but it requires more configuration.
+               
+               See: org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser
+               <mvc:annotation-driven/>
+        -->
+        
+   
        <context:annotation-config />
    
        <context:component-scan base-package="de.spring.webservices.rest"/>
                        </bean>
                </property>
                <property name="messageConverters" ref="messageConverters" />
+               
+               
+               <property name="requestBodyAdvice">
+                       <util:list>
+                               <bean id="requestBodyAdvice" class="org.springframework.web.servlet.mvc.method.annotation.JsonViewRequestBodyAdvice"/>
+                       </util:list>
+               </property>
+               
+               
                <property name="responseBodyAdvice">
                        <util:list>
                                <bean id="responseBodyAdvice" class="org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice"/>
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 {