In my case I was sending in correct object in ResponseEntity<>().
Correct :
@PostMapping(value = "/get-customer-details", produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<?> getCustomerDetails(@RequestBody String requestMessage) throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("customerId", "123");
jsonObject.put("mobileNumber", "XXX-XXX-XXXX");
return new ResponseEntity<>(jsonObject.toString(), HttpStatus.OK);
}
Incorrect :
return new ResponseEntity<>(jsonObject, HttpStatus.OK);
Because, jsonObject's toString() method "Encodes the jsonObject as a compact JSON string". Therefore, Spring returns json string directly without any further serialization.
When we send jsonObject instead, Spring tries to serialize it based on produces method used in RequestMapping and fails due to it "Could not find acceptable representation".