spring mvc配置ObjectMapper忽略无法识别的字段

转 https://blog.csdn.net/liaonanfeng88/article/details/138206038

近期参加了个 JSP+SpringMVC 3.2.x 框架的老项目,过虑设置新增接口 POST 请求传递实体 ,报了如下异常:

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

大致处理方式如下:

通过Spring的MethodInvokingFactoryBean类实现的调用configure方法,此方法返回调用该方法的本身实例。

配置完毕后,可以在spring mvc的消息处理器中使用,为了同时可以注入其实受spring管理的类,以前配置放在全局的spring文件applicationContext文件中

 

<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">
                <!-- 处理responseBody 里面日期类型,可能线程不安全,后期研究 -->
                <!-- <property name="dateFormat">
                    <bean class="java.text.SimpleDateFormat">
                        <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
                    </bean>
                </property> -->
                <!-- 为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>

 2、https://my.oschina.net/jiangzhixiong/blog/1359282

<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>
 

 

posted @ 2025-06-02 02:10  zbjice  阅读(49)  评论(0)    收藏  举报