My requirement is to use Apache CXF Rest Client API vendor has provided an url http://test.com
Method: get
Url: /getDetails
Header set to application/json
and parameter z=12345
And Response as 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"
}
]
}
When I read the documentation for the Client API I see link: http://cxf.apache.org/docs/jax-rs-client-api.html#JAX-RSClientAPI-CXFWebClientAPI WebClient approach: if i go by this example, it describes about Book() how do I describe Book object for my requirement?
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);
Also I see usage of Proxy: It talks about BookStore.class ..wont this be the server object? if so I will not be able to create or have BookStore class or object at my end.
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();
Should I create a Object similar to Book() for my response? I actually have to read each value from the JSON response (jettison). Which approach should I follow for my reqruiement and how should I proceed. I am confused please advice.
My requirement is strict to use Apache CXF Rest API.
Yes. Create a plain pojo model on your client end similar to your Entity class at the server end with same fields as needed. Also json can be marshaled directly to objects.
Let say we use Book model
To retrieve a Book object with id of 1:
WebClient client = WebClient.create("http://localhost:8084/appname/rest/");
Book book = client.path("book/" + 1 ).accept("application/json").get(Book.class);
To retrieve all Book objects:
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));
POST a Book object:
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
Update a Book object
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
NB
The client.back(true) reverts to the rest API endpoint address
The client.path(String) appends the String value to the endpoint address