Configuration d'ObjectMapper dans Spring MVC pour ignorer les champs non reconnus

Récemment, j'ai travaillé sur un ancien projet utilisant le framework JSP+SpringMVC 3.2.x. Lors de l'ajout d'une nouvelle interface avec des requêtes POST pour transmettre des entités, l'exception suivante a été levée :

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "id" (class com.trs.dto.sync.GroupSyncDTO), not marked as ignorible (12 known properties: "groupDesc", "status", "groupFullPath", "operator", "parentId", "deleted", "groupId", "groupCode", "groupOrder", "tenantId", "groupName", "email")

Voici la méthode de traitement générale :

On utilise la classe MethodInvokingFactoryBean de Spring pour appeler la méthode configure, qui retourne l'instance elle-même.

Une fois la configuration terminée, on peut l'utiliser dans le processeur de messages de Spring MVC. Pour pouvoir injecter des classes gérées par Spring, la configuration précédente était placée dans le fichier applicationContext global.

<mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8"/>
                <property name="writeAcceptCharset" value="false"/>
            </bean>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper" ref="jacksonObjectMapper"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
 
    <bean id="jacksonObjectMapper" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject">
            <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                <!-- Gestion des types de date dans responseBody, potentiellement non thread-safe, étude nécessaire -->
                <!-- <property name="dateFormat">
                    <bean class="java.text.SimpleDateFormat">
                        <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
                    </bean>
                </property> -->
                <!-- Ne pas afficher les champs null -->
                <property name="serializationInclusion">
                    <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
                </property>
            </bean>
        </property>
        <property name="targetMethod" value="configure" />
        <property name="arguments">
            <list>
                <value type="com.fasterxml.jackson.databind.DeserializationFeature">FAIL_ON_UNKNOWN_PROPERTIES</value>
                <value>false</value>
            </list>
        </property>
    </bean>

  1. Approche alternative :
<mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                        <property name="featuresToDisable">
                            <array>
                                <util:constant static-field="com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES" />
                            </array>
                        </property>
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
 

Étiquettes: Spring MVC ObjectMapper Jackson désérialisation Configuration

Publié le 9 juin à 09h06