Retrieving a JSON body from DB gives extra special characters - Spring Boot
We are saving a JSON response body in our Oracle DB which is later retrieved for the viewing purpose.
This is the JSON saved in DB
{"parameters": [{"name": "SEATS", "value": "Q1"}, {"name": "SEATS", "value": "Q2"}, {"name": "MEMBERS", "value": "Alen"}, {"name": "MEMBERS", "value": "Sara"}], "messageTemplate": "You have booked tickets, seat numbers are Q1,Q2", "url": "", "remarks": "ticket booked"}
Now the response of our view API is like this - (with an extra slash)
{
"responseBody": "{\"parameters\": [{\"name\": \"SEATS\", \"value\": \"Q1\"}, {\"name\": \"SEATS\", \"value\": \"Q2\"}, {\"name\": \"MEMBERS\", \"value\": \"Alen\"}, {\"name\": \"MEMBERS\", \"value\": \"Sara\"}], \"messageTemplate\": \"You have booked tickets, seat numbers are Q1,Q2\", \"url\": \"\", \"remarks\": \"ticket booked\"}"
}
Inspected value of the variable while debugging
{"parameters": [{"name": "SEATS", "value": "Q1"}, {"name": "SEATS", "value": "Q2"}, {"name": "MEMBERS", "value": "Alen"}, {"name": "MEMBERS", "value": "Sara"}], "messageTemplate": "You have booked tickets, seat numbers are Q1,Q2", "url": "", "remarks": "ticket booked"}
DB side
Columns description : VARCHAR2(500)
Update regarding usage of JSONObject
I tried printing it System.out.println(new JSONObject(responseBody));
like this. The console is printing the proper JSON body, but even if I use the same above logic there are still special characters.
Spring Boot part Controller
@RestController
@RequestMapping("v1/booking")
public class BookingReportsController {
@Autowired
private BookingReportsService service;
@GetMapping(value = "/more-info/{tnxId}", produces = MediaType.APPLICATION_JSON_VALUE)
public Booking moreInfo(@PathVariable(name = "tnxId") String tnxId) {
return service.moreInfo(tnxId);
}
}
We tried returning both Booking
and ResponseEntity<Booking>
but the results were the same.
This is our response bean
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Booking implements Serializable {
private static final long serialVersionUID = 7480161196626214557L;
private String txnId;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern =Constants.SETTLEMENT_PDF_DATE_FORMAT)
private Date txnDate;
private String userId;
private String appId;
private String appName;
private double amount;
private int status;
private String responseBody;
private String url;
// getters and setters
}
We are getting an extra \ as an escape character, now if we beautify the JSON and put it in multiple lines we'll get \r
& \n
in the response. So how can we remove that?
TL;DR
Just annotated private String responseBody;
with @JsonRawValue
as follows:
@JsonRawValue
private String responseBody;
Explanation
Because the value of responseBody
is a JSON string and @RestController
is going to serialize the return object of moreInfo
into HttpResponse automatically. That's why you got "extra special characters" in your response body!
Therefore, the simplest way (by using Jackson
) is to annotate those fields which you don't want to be serialized again with @JsonRawValue
.
For more information, please refer to @JsonRawValue.