ソースを参照

feat:添加配置忽略json对象属性无法对应@RequestBody类属性

解决JSON parse error: Unrecognized field "xxx"异常问题
刘传伟 1 ヶ月 前
コミット
96576c4a33
1 ファイル変更20 行追加8 行削除
  1. 20 8
      src/main/webapp/WEB-INF/spring-servlet.xml

+ 20 - 8
src/main/webapp/WEB-INF/spring-servlet.xml

@@ -11,17 +11,29 @@
 	<mvc:annotation-driven>
 		<mvc:message-converters>
 			<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
-				<property name="objectMapper">
-					<bean class="com.fasterxml.jackson.databind.ObjectMapper">
-						<!-- 为null字段时不显示 -->
-						<property name="serializationInclusion">
-							<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
-						</property>
-					</bean>
-				</property>
+				<property name="objectMapper" ref="objectMapper" />
 			</bean>
 		</mvc:message-converters>
 	</mvc:annotation-driven>
+	<bean id="objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper">
+		<!-- 为null字段时不显示 -->
+		<property name="serializationInclusion">
+			<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
+		</property>
+	</bean>
+	<!-- 使用 MethodInvokingFactoryBean 来调用 configure 方法 -->
+    <!-- 解决JSON parse error: Unrecognized field "xxx"异常问题 -->
+	<bean id="configureObjectMapper" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
+		<property name="targetObject" ref="objectMapper"/>
+		<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>
+
 	<!-- 设置使用注解的类所在的jar包 -->
 	<context:component-scan base-package="com.anyway.favor.**.controller"/>
 	<!-- 拦截器 -->