Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
EclipseLink/Examples/REST/GettingStarted/RestClient
Contents
Overview
In this post we will examine how a client may interact with the RESTful service we created in part 4.
REST & URIs
You use URIs to interact with a RESTful service, so lets examine what are URIs will look like. The following URI corresponds to our read method:
Protocol - HTTP://
We are running our RESTful service on the HTTP protocol. This is the most commonly used protocol used with RESTful services.
Host/Port - localhost:8080
For this example we used the GlassFish application server. I installed it locally on the default port of 8080. This portion will vary depending upon your particular application server setup.
Context Root - /CustomerService
The context root is set when the application is deployed. If you are using GlassFish you can use the Administration Console to find/change the context root for your application.
URL Pattern - /rest
In the WEB-INF/web.xml file we created in part 4 we specified that all URLs following the pattern "/rest/*" would correspond to our RESTful service.
<SERVLET-MAPPING> <SERVLET-NAME>Jersey Web Application</SERVLET-NAME> <URL-PATTERN>/rest/*</URL-PATTERN> </SERVLET-MAPPING>
Service URI - /customers
In our CustomerService session bean we created in part 4 we specified that our RESTful service would use the path "/customers" using the @Path annotation.
@Path("/customers") public class CustomerService { ... }
Parameter URI - /1
In the read method we specified that the URI fragment following /customers corresponds to the id parameter.
@GET @Produces(MediaType.APPLICATION_XML) @Path("{id}") public Customer read(@PathParam("id") long id) { return entityManager.find(Customer.class, id); }
Using a Web Browser as the Client
The easiest way to test our RESTful service is using your favourite web browser. Simply enter the URL and the browser will render the result as XML.
Using Java SE
There currently isn't a standard set of APIs yet for interacting with JAX-RS. However your Java SE install already has all the necessary APIs.
String uri = "http://localhost:8080/CustomerService/rest/customers/1"; URL url = new URL(uri); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", "application/xml"); JAXBContext jc = JAXBContext.newInstance(Customer.class); InputStream xml = connection.getInputStream(); Customer customer = (Customer) jc.createUnmarshaller().unmarshal(xml); connection.disconnect();
Using Implmentation Specific APIs
While there isn't a standard set of APIs to interact with JAX-RS services, implementations like Jersey do provide APIs that are easier to use than standard Java SE.
Client client = Client.create(); String uri = "http://localhost:8080/CustomerService/rest/customers/1"; WebResource resource = client.resource(uri); ClientResponse response = resource.accept("application/xml").get(ClientResponse.class); System.out.println(response.getEntity(String.class))
Summary
In this series we have combined several different Java EE technologies to create a standards based RESTful service.