如何使用javax.ws.rs.client.WebTarget从REST客户端发送json对象 [英] how to send json object from REST client using javax.ws.rs.client.WebTarget

查看:2410
本文介绍了如何使用javax.ws.rs.client.WebTarget从REST客户端发送json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面给出了一个POJO,我希望将其作为JSON或XML输出到服务器。

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

这就是我所做的

客户:

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

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

Examples I found on web were using WebResource.

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

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

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.

谢谢

推荐答案

有两个不同的泽西岛主要版本,1.x和2.x,你似乎尝试使用两者的组合,这是行不通的。 2.x版本没有像1.x那样的类,反之亦然。

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 2.x,那么你应该使用 回复 ,而不是 ClientResponse

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

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




  • 请参阅使用2.x的客户端API

  • 同样如上一篇文章中所述,getter和setter应该公开朋友

  • 另请参阅 WebTarget API

    • See Working with the Client API for 2.x
    • Also as mentioned in your previous post, the getters and setters should be public for the Friend class
    • Also see the WebTarget API
    • 基本细目。

      Invocation.Builder builder = target.request();
      


    • 我们致电 put ,我们收到回复

      Response response = builder.put(Entity.json(friend));
      


    • 要从响应中提取已知类型,我们可以使用 readEntity(类类型)

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


    • 这篇关于如何使用javax.ws.rs.client.WebTarget从REST客户端发送json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆