[JAX-RS] 4 ways to implement a client for web services

1. URLConnection client

package com.vvirlan;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class URLConnectionClient {

	public static void main(String[] args) throws IOException {
		URL restURL = new URL("http://localhost:8080/simple-service-webapp/resources/myresource/usernamepwdcontext");
		URLConnection connection = (URLConnection) restURL.openConnection();
		connection.setDoOutput(true);
		connection.connect();
		InputStreamReader ins = new InputStreamReader(connection.getInputStream());
		BufferedReader in = new BufferedReader(ins);
		String inputLine;
		while ((inputLine = in.readLine()) != null) {
			System.out.println(inputLine);
		}
		in.close();
	}
}

2. HttpConnection client

package com.vvirlan;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpConnectionClient {

	public static void main(String[] args) throws IOException {
		URL restURL = new URL("http://localhost:8080/simple-service-webapp/resources/myresource/usernamepwdcontext");
		HttpURLConnection connection  = (HttpURLConnection) restURL.openConnection();
		connection.setRequestMethod("GET");
		connection.setReadTimeout(10000);
		connection.connect();
		InputStreamReader ins = new InputStreamReader(connection.getInputStream());
		BufferedReader in = new BufferedReader(ins);
		String inputLine;
		while ((inputLine = in.readLine())!=null) {
			System.out.println(inputLine);
		}
		
	}
}

3. URL stream client

package com.vvirlan;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class URLOpenClient {

	public static void main(String[] args) throws IOException {
		URL restURL = new URL("http://localhost:8080/simple-service-webapp/resources/myresource/usernamepwdcontext");
		InputStreamReader ins = new InputStreamReader(restURL.openStream());
		BufferedReader in = new BufferedReader(ins);
		String inputLine;
		while ((inputLine = in.readLine()) != null) {
			System.out.println(inputLine);
		}
		in.close();

	}
}

4. Jersey client.

package com.vvirlan;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

public class URLJerseyClient {

	public static void main(String[] args) {
		Client cl = ClientBuilder.newClient();
		WebTarget target = cl.target("http://localhost:8080/simple-service-webapp/resources/myresource/usernamepwdcontext");
		target.path("resource");
		Builder requestBuilder = target.request();
		Response response = requestBuilder.get();
		System.out.println(response.getStatus());
		System.out.println(response.readEntity(String.class));
	}
}

For this one you will need a dependency:

<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.vvirlan</groupId>
	<artifactId>cert</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Client</name>

	<dependencies>
		<dependency>
			<groupId>org.glassfish.jersey.core</groupId>
			<artifactId>jersey-client</artifactId>
			<version>2.21</version>
		</dependency>
	</dependencies>
</project>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s