URL参数不被curl POST传递 [英] URL parameters are not being passed by curl POST

查看:1015
本文介绍了URL参数不被curl POST传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的java代码:

This is my java code:

@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

回答后,我更改了我的请求:

After the answer, I changed my request to:

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

但我仍然有同样的问题。这是来自服务器的响应:

but I still have the same problem. Here is the response from the server:

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

您可以在结尾看到零:

推荐答案

-dx = 1& y = 2 (注意 = ,而不是)是表单数据( application / x-www-form-urlencoded )发送请求的正文,其中您的资源方法应该更像



-d x=1&y=2 (notice the =, not :) is form data (application/x-www-form-urlencoded) sent it the body of the request, in which your resource method should look more like

@POST
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String sumPost(@FormParam("x") int x,
                      @FormParam("y") int y) {

}

并且以下请求会起作用


curl -XPOSThttp:// localhost:8080 / CurlServer / curl / curltutorial / sumPost-d'x = 5& y = 3'

curl -XPOST "http://localhost:8080/CurlServer/curl/curltutorial/sumPost" -d 'x=5&y=3'

x = 5& y = 3)

Note: With Windows, double quotes are required ("x=5&y=3")

您甚至可以分隔键值对


curl -XPOSThttp:// localhost:8080 / ...-d'x = 5'-d'y = 3'

默认 Content-Type application / x-www-form-urlencoded ,因此您不需要设置它。

The default Content-Type is application/x-www-form-urlencoded, so you don't need to set it.

@QueryParam 应该是查询字符串(部分URL),而不是主体数据的一部分。因此,您的请求应该更像

@QueryParams are supposed to be part of the query string (part of the URL), not part of the body data. So your request should be more like


curlhttp:// localhost:8080 / CurlServer / curl / curltutorial / sumPost?x = 1& y = 2

在正文中的任何数据,你应该可以只是使资源方法 GET 方法。

With this though, since you are not sending any data in the body, you should probably just make the resource method a GET method.

@GET
@Path("/sumPost")
@Produces(MediaType.TEXT_PLAIN)
public String sumPost(@QueryParam("x") int x,
                      @QueryParam("y") int y) {
}

发送JSON,那么您最好的选择是确保您有一个JSON提供程序 [ 1 ] ,用于处理反序列化到POJO。然后你可以有像

If you wanted to send JSON, then your best bet is to make sure you have a JSON provider[1] that handle deserializing to a POJO. Then you can have something like

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提供者。如果没有,您会收到一个异常,如没有MessageBodyReader找到mediatype应用程序/ json和类型操作数。我需要知道什么Jersey版本,如果你使用Maven或不,能够确定你应该如何添加JSON支持。但对于一般信息,您可以看到

[1]- The important thing is that you do have a JSON provider. If you don't have one, you will get an exception with a message like "No MessageBodyReader found for mediatype application/json and type Operands". I would need to know what Jersey version and if you are using Maven or not, to able to determine how you should add JSON support. But for general information you can see

  • Unmarshal JSON to Java POJO in JAX-RS

这篇关于URL参数不被curl POST传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆