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