RESTful Java client with Jersey client
This tutorial show you how to use Jersey client APIs to create a RESTful Java client to perform “GET” and “POST” requests to REST service that created in this “Jersey + Json” example.
1. Jersey Client Dependency
To use Jersey client APIs, declares “jersey-client.jar” in your pom.xml file.
File : pom.xml
<dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.8</version> </dependency>
2. GET Request
Review last REST service.
@Path("/json/metallica") public class JSONService { @GET @Path("/get") @Produces(MediaType.APPLICATION_JSON) public Track getTrackInJSON() { Track track = new Track(); track.setTitle("Enter Sandman"); track.setSinger("Metallica"); return track; } //...
Jersey client to send a “GET” request and print out the returned json data.
package com.mkyong.client; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; public class JerseyClientGet { public static void main(String[] args) { try { Client client = Client.create(); WebResource webResource = client .resource("http://localhost:8080/RESTfulExample/rest/json/metallica/get"); ClientResponse response = webResource.accept("application/json") .get(ClientResponse.class); if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } String output = response.getEntity(String.class); System.out.println("Output from Server .... \n"); System.out.println(output); } catch (Exception e) { e.printStackTrace(); } } }
Output…
Output from Server .... {"singer":"Metallica","title":"Enter Sandman"}
3. POST Request
Review last REST service.
@Path("/json/metallica") public class JSONService { @POST @Path("/post") @Consumes(MediaType.APPLICATION_JSON) public Response createTrackInJSON(Track track) { String result = "Track saved : " + track; return Response.status(201).entity(result).build(); } //...
Jersey client to send a “POST” request, with json data and print out the returned output.
package com.mkyong.client; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; public class JerseyClientPost { public static void main(String[] args) { try { Client client = Client.create(); WebResource webResource = client .resource("http://localhost:8080/RESTfulExample/rest/json/metallica/post"); String input = "{\"singer\":\"Metallica\",\"title\":\"Fade To Black\"}"; ClientResponse response = webResource.type("application/json") .post(ClientResponse.class, input); if (response.getStatus() != 201) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } System.out.println("Output from Server .... \n"); String output = response.getEntity(String.class); System.out.println(output); } catch (Exception e) { e.printStackTrace(); } } }
Output…
Output from Server .... Track saved : Track [title=Fade To Black, singer=Metallica]
Download Source Code
References
- JSON example with Jersey + Jackson
- Jersey client examples
- RESTful Java client with RESTEasy client framework
- RESTful Java client with java.net.URL
- RESTful Java client with Apache HttpClient







[...] cases I found the Apache Jersey client open source library quite convenient for implementing a RESTful client. Jersey is based on JAX-RS Java community standard, and offers easy handling of various flavors of [...]
I have spent nearly 20 hours to get “JSONP” to work with Jersey to overcome the cross-domain issue.
Passing on…
Here is the JavaScript client side:
Here is the Java Server side
The trick is to match the callback parameter name (callback in this case) and to return a JSONWithPadding Object
The server side should be improved to return a collection instead of a simple Track object
hi thx for the sample this is a good example for basic understanding..
there is a small problem in sample we need to add @XmlRootElement in Track.call to make this work
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Track {
for example a’ve method in service like this
@POST
@Path(“/login”)
@Consumes(MediaType.APPLICATION_JSON)
public Response checkUser(String username, String password) {
………………….
}
How can i send multiple parameter from client. Thank you..
Thanks a lot for such easy and complete tutorial.
Simplicity makes it easy to understand.
Thanks for this great tutorial. Very much appreciated.
Lan