即使一切看起來都不錯,我也找不到為什么我會從 Jacksoin 那里得到一個例外。
我有以下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
}
我需要將 Json 反序列化為:
@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;
}
}
當我打電話時,ObjectMapper().readValue(json, LogMdto.class)我得到以下例外;
無法識別的欄位“device_id”(類 com.plintech.LorikeetNestSpring.models.dto.mobile.LogMdto),未標記為可忽略
我不明白為什么這個例外。現場deviceId與 @JsonProperty("device_id") 映射,我有 setter 和 getter 但我仍然出錯。
更新
我做了以下更改:
//Removed the annotation
private String deviceId;
…………
將 setter 和 getter 更改如下:
@JsonProperty(value = "device_id")
public String getDeviceId() {
return deviceId;
}
@JsonProperty(value = "device_id")
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
但結果總是相同的例外,我不明白我的代碼有什么問題,我無法弄清楚我還能嘗試什么來確保沒有其他隱藏的問題。
uj5u.com熱心網友回復:
如果我沒記錯的話,因為它是一個私有欄位,那么@JsonProperty不會使用Field注釋本身,而是使用 Getter/Setter。
Getter/Setter 將用于序列化/反序列化,并且在這兩種情況下它們都不匹配device_id名稱;
修復它:添加@JsonProperty到您的 Getter/Setter。
另一方面,如果它是一個公共變數,那么下面沒有 Getter/Setter 的情況下可以正常作業:
@JsonProperty("device_id")
public String deviceId;
=== 測驗代碼 ===
@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()); }
日志:
0BC813D9-37FE-4FAB-9FA0-05F85CBAE777
uj5u.com熱心網友回復:
蘇珊的回答是正確的。
你必須將@JsonProperty("device_id")加到你的 getter 和 setter 中。
為避免出現此類錯誤,您始終可以使用 aAnySetter來保留無法正確映射的資料。
例如:
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);
}
....
uj5u.com熱心網友回復:
好的,謝謝大家的幫助,但答案有點微妙。代碼沒有錯,問題是mapper不對,是誤匯入了
org.codehaus.jackson
版本而不是
更快的xml
.... 改變了匯入指令...一切又好起來了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/355786.html
