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

前端 未结 2 1911
感情败类
感情败类 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:30

    If your web-service produces a JSON you must handle that in your client by using an accept() method:

    ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON).post(searchQuery, MediaType.APPLICATION_JSON);
    ListWrapper listWrapper = response.getEntity(ListWrapper.class);
    

    Try this and give your results.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题