[英]how do i make DTO to process the following response in springboot
{
"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;
但它拋出了一個異常
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of
java.util.HashMap
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{ :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 < 1.24 < 2.4 < 4.3"}'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance ofjava.util.HashMap
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{: 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 < 1.24 < 2.4 < 4.3"}')
我應該如何讓 DTO 處理即將到來的數據?
您的 dto 缺少構造函數、getter 和 setter
假設您使用 Jackson 反序列化 JSON,您可以通過在 DTO 中包含設置器來解決此問題:
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;
}
}
像這樣使用。
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.