This tutorial will show you an example on how to test one of the features of webservice called language independence.

Webservice is a technology by which two or more remote web applications interact with each other over network/internet. It can be implemented using Java, .net, PHP etc.

Please go through the REST webservice created using Codeigniter framework Working with RESTful services in Codeigniter – GET example

So we will call the above REST webservice created in PHP Codeigniter framework using Java language. So basically the Java language will works as a client part of the deployed service in PHP server.

Please go through the below steps to create a REST client using Jersey API

Step 1. Create a standalone project in Eclipse. Modify the pom.xml file according to the below source code

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.roytuts</groupId>
	<artifactId>rest</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>rest Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<properties>
		<java.version>1.8</java.version>
		<jersey.version>2.23.2</jersey.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.glassfish.jersey.containers</groupId>
			<artifactId>jersey-container-servlet</artifactId>
			<version>${jersey.version}</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<finalName>rest</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Step 2. Create the below class to invoke the service method which is running on PHP server.

package com.roytuts.rest.client;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;

public class RestClient {
	public static void main(String[] args) {
		Client client = ClientBuilder.newClient();
		String response = client.target("http://localhost/ci_3_1_0/index.php/restgetcontroller").path("contacts")
				.request(MediaType.APPLICATION_JSON).get(String.class);
		System.out.println(response);
	}
}

Step 3. Run the above class, you will see the below output in the console

[
	{"contact_id":"1","contact_name":"S Roy","contact_address":"http:\/\/roytuts.com","contact_phone":"1234578960"},
	{"contact_id":"2","contact_name":"S Roy","contact_address":"http:\/\/roytuts.com","contact_phone":"1234578960"},
	{"contact_id":"3","contact_name":"S Roy","contact_address":"http:\/\/roytuts.com","contact_phone":"5454544574"},
	{"contact_id":"4","contact_name":"S Roy","contact_address":"http:\/\/roytuts.com","contact_phone":"4578912360"},
	{"contact_id":"5","contact_name":"S Roy","contact_address":"http:\/\/roytuts.com","contact_phone":"8945761254"},
	{"contact_id":"6","contact_name":"S Roy","contact_address":"http:\/\/roytuts.com","contact_phone":"4587961235"},
	{"contact_id":"7","contact_name":"S Roy","contact_address":"http:\/\/roytuts.com","contact_phone":"1254897652"},
	{"contact_id":"12","contact_name":"S Roy","contact_address":"http:\/\/roytuts.com","contact_phone":"4517895621"},
	{"contact_id":"14","contact_name":"S Roy","contact_address":"http:\/\/roytuts.com","contact_phone":"1234567890"},
	{"contact_id":"15","contact_name":"S Roy","contact_address":"http:\/\/roytuts.com","contact_phone":"1234567890"}
]

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *