Home > Software design >  how do i make DTO to process the following response in springboot
how do i make DTO to process the following response in springboot

Time:12-12

{
    "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."
}

Currently I am having DTO like that:

private String code;
private String content;
private Integer ordering;
private HashMap<String,String> options;

private String correct;
private String explaination;

But it is throwing an exception as

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 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"}')

How should I make the DTO process the data coming?

CodePudding user response:

Your dto is missing constructor, getters and setters

CodePudding user response:

Assuming you are using Jackson to deserialize your JSON, you can fix this by including setters in your 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;
    }
}

CodePudding user response:

Use like this.

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;
}
  • Related