1

I have defined a method like this

@RequestMapping(value="/multiRquestBody",method=RequestMethod.POST)
    public String multiRquestBodyMethod(@RequestBody String[] body1,@RequestBody String[] body2){
        System.out.println("body1 : "+body1);
        System.out.println("body 2 : "+body2);
        return Arrays.toString(body1)+"------"+Arrays.toString(body2);
    }

I used curl command like this to call that method

curl -X POST  "http://localhost:7979/choudhury-rest/rest/book/multiRquestBody"     -d '["test","test","test"],["testing","testing string array"]' -H "Content-Type: application/json"

Then I got an error like this

The request sent by the client was syntactically incorrect.

I have tried another way like

curl -X POST  "http://localhost:7979/choudhury-rest/rest/book/multiRquestBody"     -d '["test","test","test"]&["testing","testing string array"]' -H "Content-Type: application/json"

But still, the same issue is coming how can I solve it

Ravinder
  • 41
  • 4
  • This is probably a duplicated post, please check https://stackoverflow.com/questions/12893566/passing-multiple-variables-in-requestbody-to-a-spring-mvc-controller-using-ajax – Karl Feb 22 '19 at 15:59
  • Possible duplicate of [Passing multiple variables in @RequestBody to a Spring MVC controller using Ajax](https://stackoverflow.com/questions/12893566/passing-multiple-variables-in-requestbody-to-a-spring-mvc-controller-using-ajax) – Todoy Feb 22 '19 at 17:44

1 Answers1

1

@RequestBody should ideally be used only once in the method and hold the entire body of the request. In your case, you can create an object that holds the two string arrays, so something like this:

@RequestMapping(value="/multiRquestBody",method=RequestMethod.POST)
public String multiRquestBodyMethod(@RequestBody StringArraysBody body){
    System.out.println("body1 : "+body.getBody1());
    System.out.println("body 2 : "+body.getBody2());
    return Arrays.toString(body.getBody1())+"------"+Arrays.toString(body.getBody2());
}

public class StringArraysBody {
   String[] body1;
   String[] body2;

   public String[] getBody1() {
     return body1;
   }

   public String[] getBody2() {
     return body2;
   }
}
xProgramery
  • 417
  • 4
  • 12
  • Thanks for reply I know this approach, but I got the requirement to define a method ( same as in the question ) and I need to call it So is there any way – Ravinder Feb 23 '19 at 09:27