I cannot find why I get an exception from Jacksoin even if everything looks ok.
I have the following Json:
{
"id": "8a599509-994a-4cff-ad6d-c3f770e8d159",
"action": "0",
"code": "13",
"device_id": "0BC813D9-37FE-4FAB-9FA0-05F85CBAE777",
"branch_id": "0BC813D9-37FE-4FAB-9FA0-05F85CBAE999",
"description": "Sergio sei uno stra-figo",
"date": "2021-11-09T10:22:45",
"severity": 1
}
And the class I need to deserialize the Json to:
@JsonIgnorePropenter code hereerties(ignoreUnknown = true)
public class LogMdto {
private String id;
public String getId() {
return id;
}
@JsonProperty(value = "object_id")
private String objectId;
@Size(max = Log.objectNameSize, message = "size may not by bigger than " Log.objectNameSize)
@JsonProperty(value = "object_name")
private String objectName;
@JsonProperty("device_id")
private String deviceId;
@NotNull(message = "may not be null")
private String description;
@NotNull(message = "may not be null")
@Size(max = Log.codeSize,
message = "size may not by bigger than " Log.codeSize)
private String code;
private Integer action;
private String id;
private int severity;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss")
private Timestamp date;
public Timestamp getDate() {
return date;
}
public void setDate(String date) {
DateTimeFormatter dft = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
this.date = Timestamp.valueOf(LocalDateTime.from(dft.parse(date)));
}
public String getObjectId() {
return objectId;
}
public void setObjectId(String objectId) {
this.objectId = objectId;
}
public String getObjectName() {
return objectName;
}
public void setObjectName(String objectName) {
this.objectName = objectName;
}
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Integer getAction() {
return action;
}
public void setAction(Integer action) {
this.action = action;
}
public void setId(String id) {
this.id = id;
}
public int getSeverity() {
return severity;
}
public void setSeverity(int severity) {
this.severity = severity;
}
}
When I call ObjectMapper().readValue(json, LogMdto.class)
I get the following excepion;
Unrecognized field "device_id" (Class com.plintech.LorikeetNestSpring.models.dto.mobile.LogMdto), not marked as ignorable
I do not understand why this exception. The field deviceId is mapped with @JsonProperty("device_id"), I have setter and getter but I still an error.
UPDATE
I have done the following changes:
//Removed the annotation
private String deviceId;
.... ....
Changed setter and getter as follows:
@JsonProperty(value = "device_id")
public String getDeviceId() {
return deviceId;
}
@JsonProperty(value = "device_id")
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
But the result is always the same exception, I don't understand what is wrong with my code and I cannot figure out what else I can try to make sure there aren't other hidden issues.
CodePudding user response:
If I am not mistaken, since it is a private field, then the Field @JsonProperty
annotation itself is not used, but rather the Getter/Setter.
The Getter/Setter will be used for serialization/de-serialization and in both cases they do not match device_id
name;
To Fix it: Add @JsonProperty
to your Getter/Setter.
On the other hand, if it was a public variable, then the below without Getter/Setter would work fine:
@JsonProperty("device_id")
public String deviceId;
=== Tested Code ===
@JsonIgnoreProperties(ignoreUnknown = true)
public class LogMdto {
private String id;
@JsonProperty(value = "object_id")
private String objectId;
@JsonProperty(value = "object_name")
private String objectName;
private String deviceId;
@NotNull(message = "may not be null")
private String description;
@NotNull(message = "may not be null")
private String code;
private Integer action;
private int severity;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
private Timestamp date;
public String getId() {
return id;
}
public Timestamp getDate() {
return date;
}
public void setDate(String date) {
DateTimeFormatter dft = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
this.date = Timestamp.valueOf(LocalDateTime.from(dft.parse(date)));
}
public String getObjectId() {
return objectId;
}
public void setObjectId(String objectId) {
this.objectId = objectId;
}
public String getObjectName() {
return objectName;
}
public void setObjectName(String objectName) {
this.objectName = objectName;
}
@JsonProperty("device_id")
public String getDeviceId() {
return deviceId;
}
@JsonProperty("device_id")
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Integer getAction() {
return action;
}
public void setAction(Integer action) {
this.action = action;
}
public void setId(String id) {
this.id = id;
}
public int getSeverity() {
return severity;
}
public void setSeverity(int severity) {
this.severity = severity;
}
}
public static void main(String[] args) throws Exception{ String jsonTest = "{\n" " \"id\": \"8a599509-994a-4cff-ad6d-c3f770e8d159\",\n" " \"action\": \"0\",\n" " \"code\": \"13\",\n" " \"device_id\": \"0BC813D9-37FE-4FAB-9FA0-05F85CBAE777\",\n" " \"branch_id\": \"0BC813D9-37FE-4FAB-9FA0-05F85CBAE999\",\n" " \"description\": \"Sergio sei uno stra-figo\",\n" " \"date\": \"2021-11-09T10:22:45\",\n" " \"severity\": 1\n" " }"; LogMdto logMdto = new ObjectMapper().readValue(jsonTest, LogMdto.class); System.out.println(logMdto.getDeviceId()); }
Log:
0BC813D9-37FE-4FAB-9FA0-05F85CBAE777
CodePudding user response:
The answer from Susan is correct.
You have to add the @JsonProperty("device_id")
to your getters and setters.
To avoid Errors like these, you could always use a AnySetter
that keeps the data that could not be correctly mapped.
For example:
public class LogMdto {
....
private Map<String, Object> additionalProperties = new HashMap<>(); // This Map holds all the properties that were not found.
// You could check what is wrong with the names that this POJO receives from your JSON
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
....
CodePudding user response:
Ok, thank you all for your help, but the answer is a little more subtle. There is nothing wrong with the code, the problem is that the mapper is not the right one, it was imported by mistake the
org.codehaus.jackson
version instead of
fasterxml
.... changed the import directive... everything was fine again.