← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12926: minor fixes in FRED-API, use custom objectMapper directly in controller

 

------------------------------------------------------------
revno: 12926
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2013-11-12 10:51:23 +0100
message:
  minor fixes in FRED-API, use custom objectMapper directly in controller
modified:
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/FredSpringWebTest.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageUtils.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ObjectMapperFactoryBean.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ValidationUtils.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/resources/META-INF/dhis/webapi-fred.xml


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/FredSpringWebTest.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/FredSpringWebTest.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/FredSpringWebTest.java	2013-11-12 09:51:23 +0000
@@ -30,7 +30,7 @@
 
 import com.fasterxml.jackson.databind.ObjectMapper;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
-import org.hisp.dhis.web.webapi.v1.utils.ObjectMapperFactoryBean;
+import org.hisp.dhis.web.webapi.v1.utils.CustomObjectMapper;
 import org.junit.Before;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -114,7 +114,7 @@
     @Before
     public void setup() throws Exception
     {
-        objectMapper = new ObjectMapperFactoryBean().getObject();
+        objectMapper = new CustomObjectMapper();
 
         CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
         characterEncodingFilter.setEncoding( "UTF-8" );

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java	2013-11-12 09:04:21 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java	2013-11-12 09:51:23 +0000
@@ -67,8 +67,10 @@
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseStatus;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import javax.validation.ConstraintViolation;
 import javax.validation.Validator;
 import javax.validation.groups.Default;
@@ -243,13 +245,14 @@
     }
 
     @RequestMapping( value = "", method = RequestMethod.GET )
-    public ResponseEntity<Facilities> readFacilities(
+    @ResponseStatus( HttpStatus.OK )
+    public void readFacilities(
         @RequestParam( value = "updatedSince", required = false ) Date lastUpdated,
         @RequestParam( value = "allProperties", required = false, defaultValue = "true" ) Boolean allProperties,
         @RequestParam( value = "fields", required = false ) String fields,
         @RequestParam( value = "limit", required = false, defaultValue = "25" ) String limit,
         @RequestParam( value = "offset", required = false, defaultValue = "0" ) Integer offset,
-        HttpServletRequest request )
+        HttpServletRequest request, HttpServletResponse response ) throws IOException
     {
         Facilities facilities = new Facilities();
         List<OrganisationUnit> allOrganisationUnits;
@@ -301,7 +304,7 @@
             }
         }
 
-        return new ResponseEntity<Facilities>( facilities, HttpStatus.OK );
+        objectMapper.writeValue( response.getOutputStream(), facilities );
     }
 
     private Integer getLimitValue( String limit, int defaultValue )
@@ -328,10 +331,11 @@
     }
 
     @RequestMapping( value = "/{id}", method = RequestMethod.GET )
-    public ResponseEntity<Facility> readFacility( @PathVariable String id,
+    @ResponseStatus( HttpStatus.OK )
+    public void readFacility( @PathVariable String id,
         @RequestParam( value = "allProperties", required = false, defaultValue = "true" ) Boolean allProperties,
         @RequestParam( value = "fields", required = false ) String fields,
-        HttpServletRequest request ) throws FacilityNotFoundException
+        HttpServletRequest request, HttpServletResponse response ) throws FacilityNotFoundException, IOException
     {
         OrganisationUnit organisationUnit = getOrganisationUnit( id );
 
@@ -351,7 +355,7 @@
             facility.setHref( facility.getHref() + ".json" );
         }
 
-        return new ResponseEntity<Facility>( facility, HttpStatus.OK );
+        objectMapper.writeValue( response.getOutputStream(), facility );
     }
 
     private void addHierarchyPropertyToFacility( List<OrganisationUnitLevel> organisationUnitLevels, Facility facility )

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageUtils.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageUtils.java	2013-11-01 10:01:06 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageUtils.java	2013-11-12 09:51:23 +0000
@@ -31,6 +31,7 @@
 import com.fasterxml.jackson.annotation.JsonInclude;
 import com.fasterxml.jackson.core.JsonGenerator;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
 import org.hisp.dhis.web.webapi.v1.domain.MessageResponse;
 
 import java.io.IOException;
@@ -47,6 +48,7 @@
         objectMapper = new ObjectMapper();
         objectMapper.configure( JsonGenerator.Feature.ESCAPE_NON_ASCII, true );
         objectMapper.setSerializationInclusion( JsonInclude.Include.NON_EMPTY );
+        objectMapper.enable( SerializationFeature.INDENT_OUTPUT );
     }
 
     public static String jsonMessage( String message )

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ObjectMapperFactoryBean.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ObjectMapperFactoryBean.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ObjectMapperFactoryBean.java	2013-11-12 09:51:23 +0000
@@ -48,6 +48,7 @@
         objectMapper.configure( JsonGenerator.Feature.ESCAPE_NON_ASCII, true );
         objectMapper.disable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS );
         objectMapper.setSerializationInclusion( JsonInclude.Include.NON_NULL );
+        objectMapper.enable( SerializationFeature.INDENT_OUTPUT );
 
         return objectMapper;
     }

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ValidationUtils.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ValidationUtils.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ValidationUtils.java	2013-11-12 09:51:23 +0000
@@ -31,6 +31,7 @@
 import com.fasterxml.jackson.annotation.JsonInclude;
 import com.fasterxml.jackson.core.JsonGenerator;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
 
 import javax.validation.ConstraintViolation;
 import java.io.IOException;
@@ -50,6 +51,7 @@
         objectMapper = new ObjectMapper();
         objectMapper.configure( JsonGenerator.Feature.ESCAPE_NON_ASCII, true );
         objectMapper.setSerializationInclusion( JsonInclude.Include.NON_EMPTY );
+        objectMapper.enable( SerializationFeature.INDENT_OUTPUT );
     }
 
     public static <T> String constraintViolationsToJson( Set<ConstraintViolation<T>> constraintViolations ) throws IOException

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/resources/META-INF/dhis/webapi-fred.xml'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/resources/META-INF/dhis/webapi-fred.xml	2013-11-12 09:04:21 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/resources/META-INF/dhis/webapi-fred.xml	2013-11-12 09:51:23 +0000
@@ -13,6 +13,7 @@
   <sec:global-method-security pre-post-annotations="enabled" />
 
   <context:component-scan base-package="org.hisp.dhis.web.webapi" />
+
   <context:annotation-config />
 
   <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
@@ -59,6 +60,20 @@
     </constructor-arg>
   </bean>
 
+  <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
+    <property name="objectMapper" ref="objectMapperFactoryBean" />
+    <property name="modelKey" value="entity" />
+    <property name="extractValueFromSingleKeyModel" value="true" />
+  </bean>
+
+  <mvc:annotation-driven>
+    <mvc:message-converters register-defaults="false">
+      <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
+        <property name="objectMapper" ref="objectMapperFactoryBean" />
+      </bean>
+    </mvc:message-converters>
+  </mvc:annotation-driven>
+
   <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManager">
     <constructor-arg>
       <list>
@@ -73,11 +88,7 @@
 
     <property name="defaultViews">
       <list>
-        <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
-          <property name="objectMapper" ref="objectMapperFactoryBean" />
-          <property name="modelKey" value="entity" />
-          <property name="extractValueFromSingleKeyModel" value="true" />
-        </bean>
+        <ref bean="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
       </list>
     </property>
   </bean>