【问题标题】:Apache CXF Rest Client ClarityApache CXF Rest 客户端清晰度
【发布时间】:2013-09-08 07:37:32
【问题描述】:

我的要求是使用 Apache CXF Rest Client API 供应商提供了一个网址http://test.com

Method: get
Url: /getDetails
Header set to application/json
and parameter z=12345

并以 JSON 格式响应:

{
  "hasMore": "true",
  "results": [
    {
      "name": "ATM: 16th & Treemont",
      "phone": "(303) 249--‐9117",
      "streetAddress": "303 16th St. Suite 100"
    },
    {    
      "name": "ATM2:17th & Fremont",
      "phone": "(555) 999-98886",
      "streetAddress": "304 17th St. Suite 200"
    }
  ]
}

当我阅读客户端 API 的文档时,我看到 关联: http://cxf.apache.org/docs/jax-rs-client-api.html#JAX-RSClientAPI-CXFWebClientAPI WebClient 方法: 如果我通过这个例子,它描述了 Book() 我如何根据我的要求描述 Book 对象?

WebClient client = WebClient.create("http://books");
client.path("bookstore/books");
client.type("text/xml").accept("text/xml")
Response r = client.post(new Book());
Book b = r.readEntity(Book.class);   

我还看到了 Proxy 的用法:它谈到 BookStore.class ..这不是服务器对象吗?如果是这样,我将无法在最后创建或拥有 BookStore 类或对象。

BookStore store = JAXRSClientFactory.create("http://bookstore.com", BookStore.class);
// (1) remote GET call to http://bookstore.com/bookstore
Books books = store.getAllBooks();
// (2) no remote call
BookResource subresource = store.getBookSubresource(1);
// {3} remote GET call to http://bookstore.com/bookstore/1
Book b = subresource.getBook();

我应该为我的回复创建一个类似于 Book() 的对象吗?实际上,我必须从 JSON 响应 (jettison) 中读取每个值。 对于我的要求,我应该遵循哪种方法以及我应该如何进行。我很困惑请指教。

我的要求是严格使用 Apache CXF Rest API。

【问题讨论】:

    标签: apache rest cxf jettison


    【解决方案1】:

    是的。在您的客户端创建一个普通的 pojo 模型,类似于您在服务器端的 Entity 类,并根据需要使用相同的字段。 json 也可以直接编组到对象。

    假设我们使用 Book 模型

    检索 id 为 1 的 Book 对象:

    WebClient client = WebClient.create("http://localhost:8084/appname/rest/");
    
    Book book = client.path("book/" + 1 ).accept("application/json").get(Book.class);
    

    检索所有 Book 对象:

    WebClient client = WebClient.create("http://localhost:8084/appname/rest/");
    
    
    Set<Book> books = new HashSet<Book>(client.path("books/all").accept("application/json").getCollection(Book.class));
    

    发布一个 Book 对象:

    WebClient client = WebClient.create("http://localhost:8084/appname/rest/");
    
    Book book = new Book();
    
    book.setAuthor("Shiv);
    
    book.setPublishedDate(new Date());
    
    client.path("/book-post");
    
    client.post(book); // Persist object
    

    更新 Book 对象

    WebClient client = WebClient.create("http://localhost:8084/appname/rest/");
    
    Book book = client.path("book/" + 1 ).accept("application/json").get(Book.class);
    
    book.setAuthor("Gopal);
    
    book.setPublishedDate(new Date());
    
    client.back(true);
    
    client.path("/book-put");
    
    client.put(book);// update book object
    

    注意

    client.back(true) 恢复到其余 API 端点地址

    client.path(String) 将字符串值附加到端点地址

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-12
      • 2017-06-13
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多