I have a DTO like below:
@Getter
@Setter
@AllArgsConstructor
public class LightRoundResponse {
private String round;
private JSONObject fields;
}
I am able to store and fetch the JSON Object from the DB. After setting DTO's fields attribute with the ResultSet's fields, I am able to see the JSONObject containing the correct data while debugging.
However the response which I get is:
{
"round": "A Round",
"fields": {
"empty": false
}
}
fields object is incorrect, I think since it's a JSONObject it could be an issue but I am not sure.
How can I get the correct response and not "empty": false
CodePudding user response:
Converting the JSONObject to Map does the job.
Example:
public class LightRoundResponse {
private String round;
private Map fields;
public void setFields(JSONObject fields){
this.fields = fields.toMap();
}
}
I hope I could understand why JSONObject doesn't works, but seems like the community isn't aware also. Hence posting a workaround, could help people facing the same issue.
CodePudding user response:
Jackson does not know how to serialize JSONObject
class. The expected way to do that is to actually use Map<String, Object>
:
@Getter
@Setter
@AllArgsConstructor
public class LightRoundResponse {
private String round;
private Map<String, Object> fields;
}