Spring MVC @RequestParam - empty List vs null

To get Spring to give you an empty list instead of null, you set the default value to be an empty string:

@RequestParam(required = false, defaultValue = "")

This can be managed in the serialization with ObjectMapper. If you are using jackson in your spring MVC, you can do either the following.

1) Configure your object mapper:

objectMapper.configure(SerializationConfig.Feature.WRITE_EMPTY_JSON_ARRAYS, false);

2) Or if you are using beans via xml config:

<bean name="objectMapper" class="org.springframework.http.converter.json.JacksonObjectMapperFactoryBean" autowire="no">
    <property name="featuresToDisable">
        <list>
            <value type="org.codehaus.jackson.map.SerializationConfig.Feature">WRITE_EMPTY_JSON_ARRAYS</value>
        </list>
    </property>
</bean>

Tags:

Spring Mvc