Can spring map POST parameters by a way other than @RequestBody

56,273

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 "";
}
Share:
56,273
osama yaccoub
Author by

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, 2020

Comments

  • osama yaccoub
    osama yaccoub about 4 years

    I am using @RestControllers 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 ?

  • osama yaccoub
    osama yaccoub about 6 years
    Actually my question exactly is how to avoid this approach :) .... because I will need to make a DTO for every request
  • Deedar Ali Brohi
    Deedar Ali Brohi about 6 years
    yes it can be for that you have to change your request type into form-data
  • osama yaccoub
    osama yaccoub about 6 years
    you can just post the answer here so that every one gets the benefit
  • osama yaccoub
    osama yaccoub about 6 years
    also I am restricted by the frontend (Angular) sending json
  • Deedar Ali Brohi
    Deedar Ali Brohi about 6 years
    Please check my update answer, I think 2nd ANS will resolve your problem. :)
  • osama yaccoub
    osama yaccoub about 6 years
    but what if I have two parameters ?
  • osama yaccoub
    osama yaccoub about 6 years
    good ...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
    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
    Devendra Singh about 6 years
    yes If you will use map then you have to do validation manually
  • patrickjp93
    patrickjp93 about 5 years
    What 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
    Sam about 4 years
    Would you still create a POJO for one single key-value pair?