且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用http post将多个参数传递给restful webservice

更新时间:2023-11-07 21:10:10

问题在于 HTTP 请求正文中发送的 JSON 被封送至第一个方法参数.您的其他参数没有注释它们在请求中的来源,这就是您收到错误的原因:

The problem is that the JSON being sent in the HTTP Request body is marshaled to the first method parameter. Your other parameters are not annotated as to where they are coming from in the request, which is why you are getting the error:

ItemFacadeREST contains multiple parameters with no annotation.

您***的选择是将参数组合成一个 JSON 对象;类似:

Your best option would be to combine the parameters into a single JSON object; something like:

{

    "id1":[
        "string1",
        "string2"
    ],
    "id2":[
        "string3",
        "string4"
    ],
    "oo":[
        {
            "object1":""
        },
        {
            "object2":""
        }
    ]
}

然后将您的方法更改为:

And then change your method to:

 @POST
 @Path("Test3")
 @Produces("text/plain")
 @Consumes({"application/json"})
  public String Test3(MyJsonObject json) {
        //Parse the JSON object 

    }

当然,您需要定义一个表示 MyJsonObject 的类.

You will, of course, need to define a class that represents the MyJsonObject.

这篇 SO 帖子对配置 RESTful 服务有很好的解释:https://***.com/a/8194612/2378728一个>

This SO post has an excellent explanation of configuring RESTful services: https://***.com/a/8194612/2378728