您必须在 Jackson 使用的 ObjectMapper 上设置一个参数:
objMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
这是在 Spring MVC 中设置它的一种方法:
以这种方式定义自定义对象映射器:
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper(){
super.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
super.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
}
注册此自定义 ObjectMapper:
<mvc:annotation-driven >
<mvc:message-converters register-defaults="false">
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
<property name="objectMapper">
<bean class="....CustomObjectMapper"/>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
此外,在您的请求中,您需要有一个“application/json”的“Accept”标头