【发布时间】:2026-02-23 03:05:01
【问题描述】:
这是我的java代码:
@POST
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
public String sumPost(@QueryParam(value = "x") int x,
@QueryParam(value = "y") int y) {
System.out.println("x = " + x);
System.out.println("y = " + y);
return (x + y) + "\n";
}
我这样称呼它:
curl -XPOST "http://localhost:8080/CurlServer/curl/curltutorial/sumPost" -d 'x:5&y:3'
问题是System.out.println 调用一直发布零零,看来我没有正确传递 x 和 y。
更新
得到答复后,我将请求改为:
curl -d '{"x" : 4, "y":3}' "http://localhost:8080/CurlServer/curl/curltutorial/sumPost" -H "Content-Type:application/json" -H "Accept:text/plain" --include
服务是:
@POST
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
public String sumPost(@QueryParam(value = "x") int x,
@QueryParam(value = "y") int y) {
System.out.println("sumPost");
System.out.println("x = " + x);
System.out.println("y = " + y);
return (x + y) + "\n";
}
但我仍然有同样的问题。这是来自服务器的响应:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/plain
Transfer-Encoding: chunked
Date: Wed, 23 Sep 2015 11:12:38 GMT
0
你可以看到最后的零:(
【问题讨论】:
-
应该是
x=5&y=3。curl是否像您认为的那样解析:? -
@SotiriosDelimanolis 与
:和&相同 -
@SotiriosDelimanolis 请提供任何帮助
-
也许可以尝试 2 件事,1) 使用 @FormParam 2) 在 -X 和 post 之间添加一个空格
-
我也没有在手册页中看到 : 与 = 相同,也许也可以试试 curl.haxx.se/docs/manpage.html
标签: java curl jersey http-post