Consume Restful service using jersey client 2

Continue series tutorial about jersey, today i will guide you how to build a client to consume restful service usign jersey client 2. In this tutorial i also help you how to logging message request on client side. This client application will consume services provide by server side code on part 1:Build RESTful web service with jersey 2

You need following tools and techniques to begin:

  • Eclipse kepler
  • Maven
  • Jersey 2 client

1.Create Maven Java application

On Eclipse IDE, create maven project

eclipsemaven-02

Enter follow information

  • Group Id:com.devjav
  • Artifact Id:jerseyclient

2.Dependencies
We add follow dependencies to pom.xml file.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.dev.jav</groupId>
	<artifactId>jerseyclient</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>org.glassfish.jersey.core</groupId>
			<artifactId>jersey-client</artifactId>
			<version>2.7</version>
		</dependency>
		<dependency>
			<groupId>com.devjav</groupId>
			<artifactId>jersey</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
</project>

This project depenencies on the project on part 1 cause i want to reuse User. You can remove it and create your own if you don’t want using dependencies.
3. Application
We create RestClient for calling restful service

public class RestClient {
	private static Client client;
	private static RestClient instance;
	private static final String TARGET = "http://localhost:8080/jersey/services";

	private RestClient() {
		ClientConfig clientConfig = new ClientConfig();
		LoggingFilter loggingFilter = new LoggingFilter(
				Logger.getLogger("restapi"), true);
		client = ClientBuilder.newClient(clientConfig).register(
				JacksonFeature.class);
		client = client.register(loggingFilter);
	}

	public static synchronized RestClient getInstance() {
		if (instance == null) {
			instance = new RestClient();
		}
		return instance;
	}

	public User createUser(User bean) {
		WebTarget webTarget = client.target(TARGET);
		webTarget = webTarget.path("user");
		Invocation.Builder builder = webTarget.request(
				MediaType.APPLICATION_JSON_TYPE);
		User user = builder.post(
				Entity.entity(bean, MediaType.APPLICATION_JSON_TYPE),
				User.class);
		return user;

	}

	public User getUser(Long id) {
		WebTarget webTarget = client.target(TARGET);
		webTarget = webTarget.path("user");
		webTarget = webTarget.path("{id}").resolveTemplate("id", id);
		Invocation.Builder builder = webTarget.request(
				MediaType.APPLICATION_JSON_TYPE);

		User user = builder.get(User.class);
		return user;
	}

	public User updateUser(User bean) {
		WebTarget webTarget = client.target(TARGET);
		webTarget = webTarget.path("user");
		Invocation.Builder builder = webTarget.request(
				MediaType.APPLICATION_JSON_TYPE);

		User user = builder.put(
				Entity.entity(bean, MediaType.APPLICATION_JSON_TYPE),
				User.class);
		return user;
	}

}

To can see logging i create LoggingFilter and register with Jersery Client
Create Java Application entry class

public class App {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		RestClient client = RestClient.getInstance();
		User user = new User();
		user.setId(1l);
		user.setName("ThinhPT");
		client.createUser(user);
		user=client.getUser(1l);
		user.setName("ThinhPT new");
		client.updateUser(user);
	}

}

4.Run It
Run application we will see result as below

May 12, 2014 8:49:43 AM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Sending client request on thread main
1 > POST http://localhost:8080/jersey/services/user
1 > Accept: application/json
1 > Content-Type: application/json
{"id":1,"name":"ThinhPT"}

May 12, 2014 8:49:43 AM org.glassfish.jersey.filter.LoggingFilter log
INFO: 2 * Client response received on thread main
2 < 200
2 < Content-Length: 25
2 < Content-Type: application/json
2 < Date: Mon, 12 May 2014 01:49:43 GMT
2 < Server: Apache-Coyote/1.1 {"id":1,"name":"ThinhPT"} May 12, 2014 8:49:43 AM org.glassfish.jersey.filter.LoggingFilter log INFO: 3 * Sending client request on thread main 3 > GET http://localhost:8080/jersey/services/user/1
3 > Accept: application/json

May 12, 2014 8:49:43 AM org.glassfish.jersey.filter.LoggingFilter log
INFO: 4 * Client response received on thread main
4 < 200
4 < Content-Length: 25
4 < Content-Type: application/json
4 < Date: Mon, 12 May 2014 01:49:43 GMT
4 < Server: Apache-Coyote/1.1 {"id":1,"name":"ThinhPT"} May 12, 2014 8:49:43 AM org.glassfish.jersey.filter.LoggingFilter log INFO: 5 * Sending client request on thread main 5 > PUT http://localhost:8080/jersey/services/user
5 > Accept: application/json
5 > Content-Type: application/json
{"id":1,"name":"ThinhPT new"}

May 12, 2014 8:49:43 AM org.glassfish.jersey.filter.LoggingFilter log
INFO: 6 * Client response received on thread main
6 < 200
6 < Content-Length: 29
6 < Content-Type: application/json
6 < Date: Mon, 12 May 2014 01:49:43 GMT
6 < Server: Apache-Coyote/1.1
{"id":1,"name":"ThinhPT new"}

5. Source code
You can download source code here.

One thought on “Consume Restful service using jersey client 2”

  1. Pingback: Java Dev world

Leave a Reply