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

1. URLConnection client
[code language=”java”]
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();
}
}

[/code]

2. HttpConnection client
[code language=”java”]
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);
}

}
}

[/code]

3. URL stream client
[code language=”java”]
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();

}
}

[/code]

4. Jersey client.
[code language=”java”]
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));
}
}

[/code]

For this one you will need a dependency:

[code language=”xml”]
<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>
[/code]

Leave a Reply

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