August 2013
Intermediate to advanced
360 pages
10h 47m
English
JAX-RS, Restlet, and the JAX-WS @WebServiceProvider frameworks for RESTful services include client-side APIs.
These APIs are meant to simplify the task of writing RESTful clients in general, not just clients against a
RESTful service implemented in a particular framework. To underscore the point, this section takes a look
at the JAX-RS client-side API (see Example 3-18); the client, however, goes against the servlet-based predictions2 RESTful service
from Chapter 2.
Example 3-18. A sample client using the JAX-RS client-side API
packagejerseyClient;importcom.sun.jersey.api.client.Client;importcom.sun.jersey.api.client.WebResource;importjavax.ws.rs.core.MediaType;importcom.sun.jersey.api.representation.Form;publicclassJerseyClient{privatestaticfinalStringbaseUrl="http://localhost:8080/predictions2";publicstaticvoidmain(String[]args){newJerseyClient().demo();}privatevoiddemo(){Clientclient=Client.create();client.setFollowRedirects(true);// in case the service redirectsWebResourceresource=client.resource(baseUrl);getAllDemo(resource);postDemo(resource);// same resource but different verbStringurl=baseUrl+"?id=32";resource=client.resource(url);getOneDemo(resource);deleteDemo(resource);// delete id = 32}privatevoidgetAllDemo(WebResourceresource){// GET all XMLStringresponse=resource.accept(MediaType.APPLICATION_XML_TYPE).get(String.class);report("GET all in XML:\n",response);// GET all JSON
Read now
Unlock full access