How do I POST a Pojo with Jersey Client without manually convert to JSON?

前端 未结 2 1922
感情败类
感情败类 2020-12-30 08:42

I have a working json service which looks like this:

@POST
@Path(\"/{id}/query\")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(JSON)
public ListWrapper qu         


        
2条回答
  •  [愿得一人]
    2020-12-30 09:43

    The WebResource.entity(...) method doesn't alter your webResource instance... it creates and returns a Builder object that holds the change. Your call to .post is typically performed from a Builder object rather than from the WebResource object. That transition is easily obscured when all the requests are chained together.

    public void sendExample(Example example) {
        WebResource webResource = this.client.resource(this.url);
        Builder builder = webResource.type(MediaType.APPLICATION_JSON);
        builder.accept(MediaType.APPLICATION_JSON);
        builder.post(Example.class, example);
        return;
    }
    

    Here's the same example using chaining. It's still using a Builder, but less obviously.

    public void sendExample(Example example) {
        WebResource webResource = this.client.resource(this.url);
        webResource.type(MediaType.APPLICATION_JSON)
          .accept(MediaType.APPLICATION_JSON)
          .post(Example.class, example);
        return;
    }
    

提交回复
热议问题