{
"code": "ascending-order-pq-4",
"content": "Arrange the following decimal numbers in ascending order.\n4.3, 1.24, 2.4, 1.2",
"ordering": 4,
"options": {
"answer_b": "4.3 < 2.4 < 1.24 < 1.2",
"answer_a": "1.2 < 2.4 < 4.3 < 1.24",
"answer_d": "1.2 < 1.24 < 2.4 < 4.3",
"answer_c": "1.24 < 1.2 < 2.4 < 4.3"
},
"correct": "answer_d",
"explaination": "Decimals are compared the same way as multi-digit numbers by keeping the number of digits the same with the help of trailing zeroes."
}
目前我有這樣的 DTO:
private String code;
private String content;
private Integer ordering;
private HashMap<String,String> options;
private String correct;
private String explaination;
但它拋出一個例外
已解決 [org.springframework.http.converter.HttpMessageNotReadableException:JSON 決議錯誤:無法構造實體
java.util.HashMap(盡管至少存在一個創建者):沒有從字串值反序列化的字串引數建構式/工廠方法 ('{:answer_a=>" 1.2 < 2.4 < 4.3 < 1.24", :answer_b=>"4.3 < 2.4 < 1.24 < 1.2", :answer_c=>"1.24 < 1.2 < 2.4 < 4.3", :answer_d=>"1.2 < 2.3.24 < 1.24 }'); 嵌套例外是 com.fasterxml.jackson.databind.exc.MismatchedInputException:無法構造實體java.util.HashMap(盡管至少存在一個 Creator):沒有從字串值反序列化的字串引數建構式/工廠方法 ('{:answer_a=>"1.2 < 2.4 < 4.3 < 1.24", :answer_b="4.3 < 2.4 < 1。
我應該如何讓 DTO 處理即將到來的資料?
uj5u.com熱心網友回復:
假設您使用 Jackson 反序列化您的 JSON,您可以通過在 DTO 中包含 setter 來解決此問題:
public class YourDto {
private String code;
private String content;
private Integer ordering;
private HashMap<String,String> options;
private String correct;
private String explaination;
public void setCode(String code) {
this.code = code;
}
public void setContent(String content) {
this.content = content;
}
public void setOrdering(Integer ordering) {
this.ordering = ordering;
}
public void setOptions(HashMap<String, String> options) {
this.options = options;
}
public void setCorrect(String correct) {
this.correct = correct;
}
public void setExplaination(String explaination) {
this.explaination = explaination;
}
}
uj5u.com熱心網友回復:
像這樣使用。
public class ResponseDTO {
@JsonProperty("code")
private String code;
@JsonProperty("content")
private String content;
@JsonProperty("ordering")
private Integer ordering;
@JsonProperty("options")
private HashMap<String,String> options;
@JsonProperty("correct")
private String correct;
@JsonProperty("explaination")
private String explaination;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/380128.html
上一篇:為什么函式不執行特定行
