Can spring map POST parameters by a way other than @RequestBody
Solution 1
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());
Solution 2
I have change your code please check it
DTO Class
public class DTO1 {
private String idNumber;
private String applicationId;
public String getIdNumber() {
return idNumber;
}
public void setIdNumber(String idNumber) {
this.idNumber = idNumber;
}
public String getApplicationId() {
return applicationId;
}
public void setApplicationId(String applicationId) {
this.applicationId = applicationId;
}
}
Rest Controller Method
@RequestMapping(value="/requestotp",method = RequestMethod.POST)
public String requestOTP( @RequestBody DTO1 dto){
System.out.println(dto.getApplicationId()+" (------) "+dto.getIdNumber());
return "";
}
Request Type -- application/json {"idNumber":"345","applicationId":"64536"}
OR
@RequestMapping(value="/requestotp",method = RequestMethod.POST)
public String requestOTP( @RequestBody String dto){
System.out.println(dto);
return "";
}
osama yaccoub
In the software market since 2010, mostly as a developer, but also took the roles of business analyst, tech lead, tech PM and software architect during my career, I worked on large projects for the leading telecom operators in the middle east, my work also span data-centric applications, the governmental sector and the public transportation domain. I have good experience with Spring, hibernate, GWT, struts, JavaFX , oracle and MySQL DBs, architecture and design principles, integration patterns and technologies. As you might have noticed, I see myself as a real jack of all trades, I like jumping between roles, however I keep a special and secret admiration to coding :) In addition to software, I am concerned with psychology, history and children disciplines, and I spend most of my spare time -if not coding- reading on these topics. My blog: http://sw-academia.blogspot.com/
Updated on April 06, 2020Comments
-
osama yaccoub about 4 years
I am using
@RestController
s with an application where all requests arePOST
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 ? -
osama yaccoub about 6 yearsActually my question exactly is how to avoid this approach :) .... because I will need to make a DTO for every request
-
Deedar Ali Brohi about 6 yearsyes it can be for that you have to change your request type into form-data
-
osama yaccoub about 6 yearsyou can just post the answer here so that every one gets the benefit
-
osama yaccoub about 6 yearsalso I am restricted by the frontend (Angular) sending json
-
Deedar Ali Brohi about 6 yearsPlease check my update answer, I think 2nd ANS will resolve your problem. :)
-
osama yaccoub about 6 yearsbut what if I have two parameters ?
-
osama yaccoub about 6 yearsgood ...but what if I want to apply automatic validation on the parameters
@Valid
...I think I wont be able to use Map, right ? -
Deedar Ali Brohi about 6 years@osamayaccoub it will return you complete json data you have to break that using JSONOBject or any
-
Devendra Singh about 6 yearsyes If you will use map then you have to do validation manually
-
patrickjp93 about 5 yearsWhat if one of my parameters is an array of strings? I suppose I can pipe-delimit and then split on the back end, but is there a native solution?
-
Sam about 4 yearsWould you still create a POJO for one single key-value pair?