如何使用 javax.ws.rs.client.WebTarget 从 REST 客户端发送 json 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27211012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:59:14  来源:igfitidea点击:

how to send json object from REST client using javax.ws.rs.client.WebTarget

javarestjax-rsjersey-2.0jersey-client

提问by user3275095

I have a POJO given below which I want to PUT to the server as JSON or XML.

我有一个下面给出的 POJO,我想将其作为 JSON 或 XML 放到服务器上。

This is what I have done

这就是我所做的

CLIENT:

客户:

ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(getBaseURI());

public void putFriend(String uri , Friend friend)
{
    System.out.println(friend.toString());

    target = target.path(some_path).path(uri);
    ClientResponse response =        target.request(MediaType.APPLICATION_JSON).put(Entity.entity(friend,MediaType.APPLICATION_JSON),ClientResponse.class);
}

Examples I found on web were using WebResource.

我在网上找到的例子是使用 WebResource。

I don't know how to do using WebTarget. What I have done is taken from some example found on SO but Entity.entity() gives error undefined method entity(friend, String).

我不知道如何使用 WebTarget。我所做的是取自在 SO 上找到的一些示例,但 Entity.entity() 给出了错误未定义的方法实体(朋友,字符串)。

POJO

POJO

@XmlRootElement
public class Friend{

    private String friendURI;
    private String event;
    private String uri;

    String getUri() {
        return uri;
    }
    void setUri(String uri) {
        this.uri = uri;
    }
    String getFriendURI() {
        return friendURI;
    }
    void setFriendURI(String friendURI) {
        this.friendURI = friendURI;
    }
    String getEvent() {
        return event;
    }
    void setEvent(String event) {
        this.event = event;
    }
public String toString() {
        return "Friend [friendURI=" + friendURI + ", uri=" + uri + ", event=" + event
                 + "]";
}

Please guide how to do this.

请指导如何做到这一点。

Thanks

谢谢

采纳答案by Paul Samsotha

There are two different Jersey major versions, 1.x and 2.x, You seems to be trying to use a combination of both, which won't work. The 2.x versions don't have some classes as in 1.x and vice versa.

有两个不同的 Jersey 主要版本,1.x 和 2.x,您似乎正在尝试使用两者的组合,但这是行不通的。2.x 版本没有 1.x 中的某些类,反之亦然。

If you want to use Jersey 2.x, then you should be using Response, rather than ClientResponse

如果你想使用 Jersey 2.x,那么你应该使用Response, 而不是ClientResponse

Response response = target.request().put(Entity.json(friend));
                                        // .json == automatic 'application/json'

Basic breakdown.

基本分解。

  • Calling request()on WebTargetreturns an Invocation.Buidler

    Invocation.Builder builder = target.request();
    
  • Once we call put, we get back a Response

    Response response = builder.put(Entity.json(friend));
    
  • To extract a known type from the response, we could use readEntity(Class type)

    String responseString = response.readEntity(String.class);
    response.close();
    
  • 调用request()WebTarget返回一个Invocation.Buidler

    Invocation.Builder builder = target.request();
    
  • 一旦我们打电话put,我们就会得到一个Response

    Response response = builder.put(Entity.json(friend));
    
  • 要从响应中提取已知类型,我们可以使用 readEntity(Class type)

    String responseString = response.readEntity(String.class);
    response.close();