如何在curl中调用具有两个请求正文参数的端点

时间:2019-02-22 15:12:18

标签: java spring rest curl

我已经定义了这样的方法

@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);
    }

我像这样使用curl命令来调用该方法

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

然后我遇到了这样的错误

The request sent by the client was syntactically incorrect.

我尝试了另一种方式

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

但是,同样的问题来了 我该怎么解决

1 个答案:

答案 0 :(得分:1)

理想情况下,

@RequestBody在该方法中仅应使用一次,并保留请求的整个正文。在您的情况下,您可以创建一个包含两个字符串数组的对象,如下所示:

@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;
   }
}