有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Post空主体与Jersey 2客户端

我如何与Jersey 2客户提交一个空正文的post请求

final MyClass result = ClientBuilder.newClient()
    .target("http://localhost:8080")
    .path("path")
    .queryParam("key", "value")
    .request(APPLICATION_JSON)
    .post(What to fill in here if the body should be left empty??, MyClass.class);

更新:此功能:

final MyClass result = ClientBuilder
    .newBuilder().register(JacksonFeature).build()
    .target("http://localhost:8080")
    .path("path")
    .queryParam("key", "value")
    .request(APPLICATION_JSON)
    .post(null, MyClass.class);

共 (3) 个答案

  1. # 1 楼答案

    只需发布一个空的txt

       .post(Entity.text(""));
    
  2. # 2 楼答案

    我发现这对我有用:

    Response r = client
        .target(url)
        .path(path)
        .queryParam(name, value)
        .request()
        .put(Entity.json(""));
    

    传递空字符串,而不是空值

  3. # 3 楼答案

    我不知道版本是否改变了它。但是,以下方法不起作用:

    builder.put( Entity.json( null ) );

    其中,以下各项适用:

    builder.put( Entity.json( "" ) );