【问题标题】:Jersey POST request from simple terminal client来自简单终端客户端的 Jersey POST 请求
【发布时间】:2017-05-17 10:01:38
【问题描述】:

谁能告诉我如何从简单的终端客户端调用发布请求?

    @POST
    @Path("insertt")
    public void insert(@QueryParam("id") Integer id,
                       @QueryParam("name") String name,
                       @QueryParam("lastname") String lastname,
                       @QueryParam("adress") String adress,
                       @QueryParam("city") String city ) {
        Customer cust = new Customer();
        cust.setCustomerId(id);
        cust.setName(name);
        cust.setLastNAme(lastname);
        cust.setAddressline1(adress);
        cust.setCity(city);
        customerDAO.add( cust );
    }

在客户端我做:

Client c = ClientBuilder.newClient();   
WebTarget resource = c.target("http://localhost:8080/WebService/webresources/generic/insertt?id=100&name=test&lastname=test&adress=test&city=test");
//resource.request().post(); // This does not work

【问题讨论】:

  • 什么不起作用?您看到了哪个例外?
  • 你试过 curl 命令吗?
  • @MaximDobryakov 我没有看到任何异常我只是不知道如何从客户端发送请求。我有 url 和资源,但是如何从客户端发送请求?

标签: java rest jersey-2.0 jersey-client


【解决方案1】:

使用记录在here中的 curl 命令:

... 使用 curl 发布此表单,并填写与之前相同的数据,我们 可以这样做:

curl --data "birthyear=1905&press=%20OK%20" http://www.example.com/when.cgi 这种 POST 会使用 Content-Type application/x-www-form-urlencoded 是最广泛的 使用 POST 类型。

您发送到服务器的数据必须已经正确编码,curl 不会为你这样做。例如,如果您希望数据包含 一个空格,你需要用 %20 等替换那个空格。 遵守这一点很可能会导致您的数据被接收 错误和混乱。

最近的 curl 版本实际上可以为您对 POST 数据进行 url 编码,例如 这个:

curl --data-urlencode "name=I am Daniel" http://www.example.com If 你在命令行上重复 --data 几次,curl 会 连接所有给定的数据片段 - 并在之间放置一个“&”符号 每个数据段。

【讨论】:

    【解决方案2】:
    1. 因为您尝试发送 POST 数据 @QueryParam 将不起作用,因为 post 数据将作为请求正文而不是作为查询参数发送(这意味着不像您那样附加在 URL 中)。所以你必须改变你的资源方法如下:

      @POST
      @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
      @Path("insertt")
      public void insert(@FormParam("id") Integer id,
                         @FormParam("name") String name,
                         @FormParam("lastname") String lastname,
                         @FormParam("adress") String adress,
                         @FormParam("city") String city ) {
      
      Customer cust = new Customer();
      cust.setCustomerId(id);
      ...
      customerDAO.add( cust );
      

      }

    2. 并按如下方式更改您的客户端:

      Client client = ClientBuilder.newClient();
      WebTarget target = client.target("http://localhost:8080/WebService/webresources/generic").path("insertt");
      Form form = new Form().param("id", "100").param("name", "test").param("lastname", "test").param("address", "test").param("city", "test");
      Response response = target.request().post(Entity.form(form));
      

    这个例子只是模拟一个 HTML 表单提交。如果您想以 XML 或 JSON 或任何其他形式发送数据,您必须查看 JAX-RS 文档。网络上有很多资源;以下是一些示例网站:

    注意:该示例使用 Jersey 2.23 和 Wildfly 8.2.1 进行了测试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 2021-06-23
      相关资源
      最近更新 更多