Issue
In our application we are getting the request object from third party as form data. We are processing the form data in Spring controller and send the response back. In spring controller we have wrote the logic like below.
@RequestMapping(value = "/oci/html/setup", method = RequestMethod.POST,
produces = {MediaType.TEXT_HTML_VALUE }, consumes = { MediaType.APPLICATION_FORM_URLENCODED_VALUE })
@ResponseStatus(value= HttpStatus.OK)
@ResponseBody
public String handleOciSetUpRequest1(OciSetupRequest reqObject)
{
if (LOG.isDebugEnabled())
{
LOG.debug("Oci Setup Request Object: " + reqObject.toString());
}
final OciSetupResponse response = getOciService().processOciSetUpRequest(reqObject);
return response.toString();
}
Request Object:
identity: 1234
sharedSecret: password
Expected Response object:
SessionId=1236547878
URL=https://sample.com
Here we need to send the response in the form of key value pair html response. Anyone can help on this How to send the html response as key-value pair from Spring controller.
If sample code provided will be appreciated....
Thanks in advance
Solution
I see that you try to return in a response body, the best would be by means of a rest controller, and also add exception handling, with what you would have:
@RestController
public class SomeClassController {
@PostMapping("/oci/html/setup")
public ResponseEntity<?> reportRecords(OciSetupRequest reqObject) {
Map<String, Object> response = new HashMap<>();
try {
if (LOG.isDebugEnabled())
{
LOG.debug("Oci Setup Request Object: " + reqObject.toString());
}
final OciSetupResponse response = getOciService().processOciSetUpRequest(reqObject);
response.put("SessionId", "1236547878");
response.put("URL", "https://sample.com");
return new ResponseEntity<>(response, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
response.put("message", e.getMessage());
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
Answered By - Edy Huiza