jackson Unrecognized field处理方法


错误堆栈信息如下:

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "HPYS" (Class com.traffic.entity.BusBaseInfo), not marked as ignorable  
 at [Source: java.io.StringReader@76c5faf5; line: 1, column: 84] (through reference chain: com.traffic.entity.BusBaseInfoBody["result"]->com.traffic.entity.BusBaseInfo["HPYS"])  
    at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)  
    at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:246)  
    at org.codehaus.jackson.map.deser.StdDeserializer.reportUnknownProperty(StdDeserializer.java:604)  
    at org.codehaus.jackson.map.deser.StdDeserializer.handleUnknownProperty(StdDeserializer.java:590)  
    at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:689)  
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:514)  
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:350)  
    at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:120)  
    at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:97)  
    at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:26)  
    at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:252)  
    at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:356)  
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:494)  
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:350)  
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2395)  
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1595)  
    at com.trafficframework.core.util.JSONHelper.fromJsonToObject(JSONHelper.java:72)  
    at com.traffic.client.GetCarInfoUtil.main(GetCarInfoUtil.java:188)  

1、请求的JSON里面字段多余映射的实体类,可以通过在类的顶部添加@JsonIgnoreProperties,2.0版本引入

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

2、属性大小写问题,如下 属性全部大写,JackSon 默认是通过驼式命名法 处理 比如 nickName 这样儿,有时比较特殊,比如映入其他系统的字段,可能都是大写,

可以通过@JsonProperty(value = "HPHM")处理

/** 车牌号码 */  
   @JsonProperty(value = "HPHM")  
   private String HPHM;  
  
   /** 车牌颜色 */  
   @JsonProperty(value = "HPYS")  
   private String HPYS;  
  
   /** 车辆颜色 */  
   @JsonProperty(value = "CSYS")  
   private String CSYS;  
  
   /** 车辆类型 */  
   @JsonProperty(value = "CLLX")  
   private Integer CLLX;  

3、重写ObjectMapper 对象的 PropertyNamingStrategy方法 列子如下,但是不太建议,会出现不统一的问题,如果有统一规则可以使用。

final ObjectMapper mapper = new ObjectMapper();  
       mapper.setPropertyNamingStrategy(new PropertyNamingStrategy() {    
           private static final long serialVersionUID = 1L;    
           // 反序列化时调用    
           @Override    
           public String nameForSetterMethod(MapperConfig<?> config,    
                   AnnotatedMethod method, String defaultName) {    
               return method.getName().toUpperCase();  
           }    
           // 序列化时调用    
           @Override    
           public String nameForGetterMethod(MapperConfig<?> config,    
                   AnnotatedMethod method, String defaultName) {    
               return method.getName().toUpperCase();    
           }    
       });    


 

原创文章,转载请注明出处:转载自小马过河 - jackson Unrecognized field处理方法


Jbone

Spring Cloud实战项目jbone正在开发中, jbone功能包括服务管理、单点登录、系统管理平台、内容管理平台、电商平台、支付平台、工作流平台等子系统。欢迎关注!

我要关注
马军伟
关于作者 马军伟
写的不错,支持一下

先给自己定个小目标,日更一新。