场景:项目升级后突然访问接口提示com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field
原因:前端传的json对象在转化实体类bean时,有多余的属性值,解析不了
几种解决方法:
第一种:在指定类上添加属性如下属性
@JsonIgnoreProperties(ignoreUnknown = true)
第二种:全局配置
package org.activiti.rest.conf;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;/*** @author Joram Barrez*/
@Configuration
public class JacksonConfiguration {@Bean()public ObjectMapper objectMapper() {ObjectMapper mapper = new ObjectMapper();mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); return mapper;}
}
总结:前端传递参数时尽量按照后端要求的参数传递。