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
{ "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\"}" }
But when debugging its coming like this
{"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"}
Additional data
Columns description : VARCHAR2(500)
Update
I tried printing this new JSONObject(responseBody) like this
System.out.println(new JSONObject(responseBody));
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?
CodePudding user response:
you can do this
String final_json= json.replaceAll("\\\\","");
json is the string that has your json
i did four \ because if i typed one it will expect something like \n and \r
CodePudding user response:
TL;DR
Just annotated private String responseBody; with @JsonRawValue. For example:
@JsonRawValue
private String responseBody;
Explanation
Because the value of responseBody is a JSON string and @RestController is going to serialize the response of moreInfo as JSON format automatically. That's why you got "extra special characters" in your response body!
Therefore, a simple way by using Jackson is to annotate those fields which you don't want to be serialized again.
