SPRING response object 값 중에서 null 인 값은 안보내는 방법

 

spring response 를 object로 묶어서 보내는 경우, 아래와 같이 값이 null 인 경우가 있다.

{
   "resultCode": 0,
   "resultMessage": "성공",
   "results": {
     "totalCnt": 5,
     "list": [
       {
         "test": "A",
         "what": NULL,
         "who": "YENA",
         "when": NULL,
         "how": "test"
       }
     ]
   }
}

 

 

이런 경우, JSON으로 파싱에러가 발생하는 이슈 등등으로 빼고 보내고 싶은 경우, converter를 이용할 수 있다.

servlet-context.xml 에 아래의 converter를 포함시켜주자.

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                    <property name="serializationInclusion">
                        <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
                    </property>
                </bean>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

 

 

Leave a comment