Java curl POST未传递URL参数
这是我的java代码:Java curl POST未传递URL参数,java,curl,jersey,http-post,Java,Curl,Jersey,Http Post,这是我的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
@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'
curl -XPOST "http://localhost:8080/CurlServer/curl/curltutorial/sumPost?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
您可以在末尾看到零:(您缺少命令的数据部分:
curl——数据“param1=value1¶m2=value2”https://example.com/fakesite.php
-d(或--data)应该在链接之前。
“名称-值对”应该是varName=varValue&otherVar=otherValue
此外,从文档中可以看出,-X命令不正确:
This option only changes the actual word used in the HTTP request, it does not alter the way curl behaves. So for example if you want to make a proper HEAD request, using -X HEAD will not suffice. You need to use the -I, --head option.
应该是-X POST
最后,请记住使用“html编码”对您的值进行编码。-dx=1&y=2
(注意=,而不是:
)是表单数据()发送给它的请求体,其中您的资源方法应该更像
@POST
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String sumPost(@FormParam("x") int x,
@FormParam("y") int y) {
}
下面的请求也行
curl-XPOST”http://localhost:8080/CurlServer/curl/curltutorial/sumPost“-d'x=5&y=3'
注意:对于Windows,需要双引号(“x=5&y=3”
)
您甚至可以分离键值对
curl-XPOST”http://localhost:8080/...“-d'x=5'-d'y=3'
默认的内容类型
是application/x-www-form-urlencoded
,因此不需要设置它
@QueryParam
s应该是(URL的一部分),而不是正文数据的一部分
@POST
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String sumPost(@FormParam("x") int x,
@FormParam("y") int y) {
}
curl”http://localhost:8080/CurlServer/curl/curltutorial/sumPost?x=1&y=2“
尽管如此,由于您没有在主体中发送任何数据,您可能应该将资源方法设置为GET
方法
@GET
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
public String sumPost(@QueryParam("x") int x,
@QueryParam("y") int y) {
}
如果您想发送JSON,那么最好的办法是确保您有一个JSON提供程序[1],它可以处理对POJO的反序列化
public class Operands {
private int x;
private int y;
// getX setX getY setY
}
...
@POST
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
public String sumPost(Operands ops) {
}
[1]-重要的是您确实有一个JSON提供程序。如果没有,您将收到一条异常消息,如“没有为mediatype应用程序/JSON和类型操作数找到MessageBodyReader”。我需要知道Jersey的版本以及您是否使用Maven,以确定您应该如何添加JSON支持。但有关一般信息,您可以查看
您是否尝试过这样称呼它:
curl -XPOST "http://localhost:8080/CurlServer/curl/curltutorial/sumPost" -d 'x:5&y:3'
curl -XPOST "http://localhost:8080/CurlServer/curl/curltutorial/sumPost?x=5&y=3"
?可能应该是x=5&y=3
。curl
解析:
是否如您所想?@SotiriosDelimanolis与:
和&
相同任何帮助请尝试两件事,1)使用@FormParam 2)在-x和postalso之间添加一个空格,我在手册页中没有看到:与=,相同,也许你也可以尝试一下,-d
是--data
的缩写形式,并且(或者)将动词改为POST
,这样你就不需要-X
(尽管这不会造成伤害)。关键是如您所说将其放在URL之前,并使用=
。如果您使用--data urlencode
curl会为您进行URL编码。(不是HTML实体,它们像“
和和 ;
,并且不用于仅输入输出。)@dave_thompson_085我的错误,我混淆了这两个命令,将修复应答根据您的回答,请求应该是curl-X POST-d“X=4&y=2”http://localhost:8080/CurlServer/curl/curltutorial/sumPost“
但我还是有相同的problem@MarcoDinatsoli编辑您的问题,发布以下内容:http://localhost:8080/CurlServer/curl/curltutorial/sumPost
使用浏览器访问,并检查链接是否按预期工作。然后,从服务器发布访问日志,显示请求正确(200代码,来自浏览器和应用程序)。最后显示:curl--data“x=4&y=2”的结果http://localhost:8080/CurlServer/curl/curltutorial/sumPost
如图所示复制/粘贴……URL+URI不应在“
@Bonatti我已经做了所有这些,四个小时前,请检查我的更新。在回答您的问题之前,我需要澄清,您说:既然您没有在正文中发送任何数据,您可能应该将资源方法设为GET方法。
但是,等等,我正在发送post请求,这意味着x
和y
参数在主体中而不是在标题中,对吗?也许我应该说的是“因为资源方法不希望主体中有任何数据”。您没有可接受主体的方法参数。通常,接受主体的方法参数没有注释。在大多数情况下,这就是如何知道它是实体体(表单数据除外)。在您的例子中,数据应该来自URL(@QueryParam
)好的,很明显,我使用QueryParam时犯了一个错误,我不知道QueryParam代表URL。但是如果我将其更改为formparam,这是否意味着x和y在http请求的主体中?是的。并用-d
,将其发送到体内。那么第一个例子就是您将使用的。我是否理解-d'{“x”:3,“y”:4}'
并不意味着请求是json?现在加一个