org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotReadable Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized field "age1" (class com.byd.shop.entity.User), not marked as ignorable (4 known properties: "id", "sex", "age", "name"])
1、原因分析
實體類中不存在post提交的age1,從而程序拋出異常。
2、在實體類添加 @JsonIgnoreProperties(ignoreUnknown = true)
3、springweb.xml添加
<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"> <!-- 處理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> <!--//將該標記放在屬性上,如果該屬性爲NULL則不參與序列化--> <!--//如果放在類上邊,那對這個類的全部屬性起作用--> <!--//Include.Include.ALWAYS 默認--> <!--//Include.NON_DEFAULT 屬性爲默認值不序列化--> <!--//Include.NON_EMPTY 屬性爲 空(“”) 或者爲 NULL 都不序列化--> <!--//Include.NON_NULL 屬性爲NULL 不序列化--> </property> </bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
4、重啓即可。