首页 文章

发布泽西2客户端的空身

提问于
浏览
25

如何使用Jersey 2客户端提交空机构的帖子请求?

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);

Update: 这个有效:

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

5 回答

  • 5

    我可以在任何地方使用't find this in the doc',但我相信你可以使用 null 获得一个空体:

    final MyClass result = ClientBuilder.newClient()
        .target("http://localhost:8080")
        .path("path")
        .queryParam("key", "value")
        .request(APPLICATION_JSON)
        .post(Entity.json(null), MyClass.class)
    
  • 20

    我发现这对我有用:

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

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

  • 3

    我不知道版本是否会改变它 . 但是,以下不起作用:

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

    在哪里,以下工作正常:

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

  • 7

    只需发布一个空的txt .

    .post(Entity.text(""));
    
  • 0

    它只对我有用:

    .post(Entity.json("{}")
    

    所有其他解决方案仍然产生400 Bad Request

    附:请求使用MediaType.APPLICATION_JSON完成

相关问题