1

POJOs:

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;
}

Test 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);
    }
    
}

request.json :

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

Error:

Unrecognized field "dType" (class com.spring.mapstruct.test.MOEH), not marked as ignorable (3 known properties: "z", "c", "dtype"]) at [Source: (File); line: 5, column: 14] (through reference chain: com.spring.mapstruct.test.CCMTRequest["cch"]->com.spring.mapstruct.test.MOEH["dType"])

Request: null

Now, when I update my test class as :

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);
        }
        
    }

I get output as:

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

So how jackson is working here with lombok, is there an issue with properties "dType" and "uPwName" ?

4

1 回答 1

1

首先,下次请提供更好的示例而不是随机名称属性。这令人困惑。

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

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

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

  1. 为了您的快速修复:
ObjectMapper mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
  1. 为了更好地解决您的问题:为您的属性使用更好的名称。
于 2021-02-25T10:14:12.123 回答