UnrecognizedPropertyException: Unrecognized field 解决

简介: UnrecognizedPropertyException: Unrecognized field 解决

转载请注明出处:

  在项目得不同环境上对接外部的服务接口,且存在不同版本间可能有字段不同得问题,遇到这种问题在使用jackson解析时,如果格式化得字符串与定义得java类不能完全对应时,就会报错:Unrecognized field ,代码还原:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
public class CounterEntity {
    private long inoctets;
    @JsonProperty("avg-bit-rate")
    private long avgBitRate;
    @JsonProperty("avg-octet-rate")
    private long avgOctetRate;
    public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String jsonStr = "{\"pkts\":0,\"octets\":0,\"inpkts\":0,\"inoctets\":0,\"avg-bit-rate\":0,\"avg-octet-rate\":0,\"avg-packet-rate\":0,\"rt-bit-rate\":0,\"rt-octet-rate\":0,\"rt-packet-rate\":0}";
            CounterEntity entity = objectMapper.readValue(jsonStr, CounterEntity.class);
            System.out.println(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  运行main方法,报错如下:

  

  解决方案,在转换得类上使用 这个注解 @JsonIgnoreProperties(ignoreUnknown = true) ,该注解会在解析的时候,会自动忽略一些不存在得属性。通过以下方式就可以正常得序列化和反序列化。

 

  @JsonIgnoreProperties是一个Jackson库提供的注解,用于标记在序列化和反序列化过程中需要忽略的属性。它可以应用于类级别和属性级别。 在类级别上使用@JsonIgnoreProperties注解,可以指定要忽略的属性列表。例如:

@JsonIgnoreProperties({"field1", "field2"})
public class MyClass {
    private String field1;
    private String field2;
    private String field3;
    @JsonIgnoreProperties
    private String field4;
}

 

标签: java , 异常 , json

目录
相关文章
Missing required prop: “modelValue“
Missing required prop: “modelValue“
118 0
|
JSON 数据格式
遇到【Unexpected character (‘“‘ (code 34)): was expecting comma to separate Object entries】的解决办法
遇到【Unexpected character (‘“‘ (code 34)): was expecting comma to separate Object entries】的解决办法
遇到【Unexpected character (‘“‘ (code 34)): was expecting comma to separate Object entries】的解决办法
成功解决AttributeError: ‘PathCollection‘ object has no property ‘n_levels‘
成功解决AttributeError: ‘PathCollection‘ object has no property ‘n_levels‘
TypeError: custom() got an unexpected keyword argument ‘path‘
TypeError: custom() got an unexpected keyword argument ‘path‘
149 0
|
计算机视觉
成功解决TypeError: __init__() got an unexpected keyword argument 'n_iterations'
成功解决TypeError: __init__() got an unexpected keyword argument 'n_iterations'
成功解决TypeError: __init__() got an unexpected keyword argument 'serialized_options'
成功解决TypeError: __init__() got an unexpected keyword argument 'serialized_options'
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
322 0
成功解决TypeError: distplot() got an unexpected keyword argument ‘y‘
成功解决TypeError: distplot() got an unexpected keyword argument ‘y‘
|
关系型数据库 MySQL 数据库
当你遇到Error: ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect string value:
当你遇到Error: ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect string value:
|
Java 数据库连接 mybatis
Illegal overloaded getter method with ambiguous type for property
Illegal overloaded getter method with ambiguous type for property
302 0