用泽西2客户端发布空体

Sti*_*ine 28 java jersey jersey-client

如何使用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);
Run Code Online (Sandbox Code Playgroud)

更新:这有效:

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

Ald*_*den 23

我无法在任何地方的文档中找到这个,但我相信你可以null用来获得一个空洞的身体:

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

  • 多数民众赞成愚蠢.应该是一个Enity.empty()静态方法 (8认同)
  • 这种方法的问题是,泽西仍然会根据实体工厂(在本例中为`application/json`)设置内容类型头. (2认同)

小智 9

我发现这对我有用:

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

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


小智 7

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

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

在哪里,以下工作正常:

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