I am using @RestController
s with an application where all requests are POST
requests ... As I learned from this post , you can't map individual post parameters to individual method arguments, rather you need to wrap all the parameters in an object and then use this object as a method parameter annotated with @RequestBody
thus
@RequestMapping(value="/requestotp",method = RequestMethod.POST)
public String requestOTP( @RequestParam(value="idNumber") String idNumber , @RequestParam(value="applicationId") String applicationId) {
return customerService.requestOTP(idNumber, applicationId);
will not work with a POST
request of body {"idNumber":"345","applicationId":"64536"}
MY issue is that I have A LOT of POST
requests , each with only one or two parameters, It will be tedious to create all these objects just to receive the requests inside ... so is there any other way similar to the way where get request parameters (URL parameters) are handled ?
Yes there are two ways -
first - the way you are doing just you need to do is append these parameter with url, no need to give them in body. url will be like - baseurl+/requestotp?idNumber=123&applicationId=123
@RequestMapping(value="/requestotp",method = RequestMethod.POST)
public String requestOTP( @RequestParam(value="idNumber") String idNumber , @RequestParam(value="applicationId") String applicationId) {
return customerService.requestOTP(idNumber, applicationId);
second- you can use map as follows
@RequestMapping(value="/requestotp",method = RequestMethod.POST)
public String requestOTP( @RequestBody Map<String,Object> body) {
return customerService.requestOTP(body.get("idNumber").toString(), body.get("applicationId").toString());