繁体   English   中英

无法使用 Jackson 和 lombok 将 map Json 转换为 java Object

[英]Not able to map Json to java Object using Jackson and lombok

POJO:

import lombok.Data;
@Data
public class CCMTRequest {
    
    private MOEH cch;
    
    private String filler1;
    
    private CCMTCCD ccd;
    
    private String uPwName;
}


@Data
public class MOEH {
    private String c;
    private int z;
    private String dType;
}


@Data
public class CCMTCCD {
    private dTime time;
    private int x;
}

@Data
public class dTime {
    private String dTime;
}

测试Class:

public class TestJacksonParser {
    
    @Test
    void load_jsonToPOJO() {
        ObjectMapper mapper = new ObjectMapper();
        ClassLoader load = this.getClass().getClassLoader();
        File file = new File(load.getResource("request.json").getFile());
        CCMTRequest req = null;
        try {
            req = mapper.readValue(file, CCMTRequest.class);
        }
        catch(Exception e) {
            System.out.println(e.getMessage());
        }
        System.out.println("\nRequest: " + req);
    }
    
}

请求.json:

{
    "cch" : {
        "c" : "C",
        "z" : 4678,
        "dType" : "dtype"       
    },
    "filler1" : "random filler1",
    "ccd" : {
        "time" : {
            "dTime" : "4:35"
        },
        "x" : 34567
    },
    "uPwName" : "uPwName"
}

错误:

无法识别的字段“dType”(类 com.spring.mapstruct.test.MOEH),未标记为可忽略(3 个已知属性:“z”、“c”、“dtype”])在 [来源:(文件); 行:5,列:14](通过参考链:com.spring.mapstruct.test.CCMTRequest["cch"]->com.spring.mapstruct.test.MOEH["dType"])

索取:null

现在,当我将测试 class 更新为:

public class TestJacksonParser {
        
        @Test
        void load_jsonToPOJO() {
            ObjectMapper mapper = new ObjectMapper();
    
            //ignore Unknown JSON Fields
          mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

            ClassLoader load = this.getClass().getClassLoader();
            File file = new File(load.getResource("request.json").getFile());
            CCMTRequest req = null;
            try {
                req = mapper.readValue(file, CCMTRequest.class);
            }
            catch(Exception e) {
                System.out.println(e.getMessage());
            }
            System.out.println("\nRequest: " + req);
        }
        
    }

我得到 output 作为:

请求:CCMTRequest(cch=MOEH(c=C, z=4678, dType=null), filler1=random filler1, ccd=CCMTCCD(time=dTime(dTime=4:35), x=34567), uPwName=null)

那么 jackson 如何与 lombok 一起工作,属性“dType”和“uPwName”是否存在问题

首先,下次请提供更好的示例而不是随机名称属性。 这很混乱。

你的问题是因为 lombok 为像“uPwName”这样的属性生成了 getter 和 setter 变成了“getUPwName()”和“setUPwName()”。 jackson 读作“getuPwName”和“setuPwName”;

该库都对 getter 和 setter 使用不同的命名约定。

有两种方法可以解决这个问题:

  1. 为了您的快速修复:
ObjectMapper mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
  1. 为了更好地解决您的问题:为您的属性使用更好的名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM