简体   繁体   中英

Not able to map Json to java Object using Jackson and lombok

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" ?

first things first, next time please provide better example rather than random name properties. it's confusing.

your problem is because lombok generate getter and setter for property like "uPwName" becomes "getUPwName()" and "setUPwName()". jackson read it as "getuPwName" and "setuPwName";

the library both using different naming convention for getters and setters.

there are 2 approach to fix this:

  1. for your quick fix:
ObjectMapper mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
  1. for better way to fix your problem: use better name for your properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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