ws

JAX-RS service and clients

jax_rs_simple_service

  • SimpleRsService.java
  • pom.xml

jax_rs_clients

  • URLClient.java
  • URLConnectionClient.java
  • HttpURLConnectionClient.java
  • JerseyClient.java
  • pom.xml

SimpleRsService.java

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Application;

@ApplicationPath("/")
@Path("/")
public class SimpleRsService extends Application {

    @GET
    public String serverName() {
        return "SimpleRsService";
    }

    @GET
    @Path("{name}")
    public String hello(@PathParam("name") String name) {
        return "Hello: " + name;
    }
}

pom.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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.training.rs</groupId>
    <artifactId>jax_rs_simple_service</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

URLClient.java

package com.training.rs.client;

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

public class URLClient {

    public static void main(String[] args) throws Exception {
        URLClient urlClient = new URLClient();
        urlClient.invokeServerNameMethod();
        urlClient.invokeHelloMethod();
    }

    private void invokeServerNameMethod() throws Exception {
        String parseURL = "http://localhost:9080/1_0_simple_service/";
        invokeServiceMethodAndReadReturnValue(parseURL);
    }

    private void invokeHelloMethod() throws Exception {
        String parseURL = "http://localhost:9080/1_0_simple_service/user";
        invokeServiceMethodAndReadReturnValue(parseURL);
    }

    private void invokeServiceMethodAndReadReturnValue(String parseURL)
            throws Exception {
        URL url = new URL(parseURL);
        InputStreamReader inputStreamReader = new InputStreamReader(
                url.openStream());
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String inputLine;
        while ((inputLine = bufferedReader.readLine()) != null) {
            System.out.println(inputLine);
        }
        bufferedReader.close();
    }

}

URLConnectionClient.java

package com.training.rs.client;

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

public class URLConnectionClient {

    public static void main(String[] args) throws Exception {
        URLConnectionClient urlClient = new URLConnectionClient();
        urlClient.invokeServerNameMethod();
        urlClient.invokeHelloMethod();
    }

    private void invokeServerNameMethod() throws Exception {
        String parseURL = "http://localhost:9080/1_0_simple_service/";
        invokeServiceMethodAndReadReturnValue(parseURL);
    }

    private void invokeHelloMethod() throws Exception {
        String parseURL = "http://localhost:9080/1_0_simple_service/user";
        invokeServiceMethodAndReadReturnValue(parseURL);
    }

    private void invokeServiceMethodAndReadReturnValue(String parseURL)
            throws Exception {
        URL url = new URL(parseURL);
        URLConnection urlConnection = url.openConnection();
        urlConnection.setDoInput(true);
        urlConnection.setReadTimeout(10000);
        urlConnection.connect();
        InputStreamReader inputStreamReader = new InputStreamReader(
                urlConnection.getInputStream());
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String inputLine;
        while ((inputLine = bufferedReader.readLine()) != null) {
            System.out.println(inputLine);
        }
        bufferedReader.close();
    }

}

HttpURLConnectionClient.java

package com.training.rs.client;

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

public class HttpURLConnectionClient {

    public static void main(String[] args) throws Exception {
        HttpURLConnectionClient urlClient = new HttpURLConnectionClient();
        urlClient.invokeServerNameMethod();
        urlClient.invokeHelloMethod();
    }

    private void invokeServerNameMethod() throws Exception {
        String parseURL = "http://localhost:9080/1_0_simple_service/";
        invokeServiceMethodAndReadReturnValue(parseURL);
    }

    private void invokeHelloMethod() throws Exception {
        String parseURL = "http://localhost:9080/1_0_simple_service/user";
        invokeServiceMethodAndReadReturnValue(parseURL);
    }

    private void invokeServiceMethodAndReadReturnValue(String parseURL)
            throws Exception {
        URL url = new URL(parseURL);
        HttpURLConnection httpUrlConnection = (HttpURLConnection) url
                .openConnection();
        httpUrlConnection.setRequestMethod("GET");
        httpUrlConnection.setDoInput(true);
        httpUrlConnection.setReadTimeout(10000);
        httpUrlConnection.connect();
        InputStreamReader inputStreamReader = new InputStreamReader(
                httpUrlConnection.getInputStream());
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String inputLine;
        while ((inputLine = bufferedReader.readLine()) != null) {
            System.out.println(inputLine);
        }
        bufferedReader.close();
    }

}

JerseyClient.java

package com.training.rs.client;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

public class JerseyClient {

    public static void main(String[] args) throws Exception {
        JerseyClient urlClient = new JerseyClient();
        urlClient.invokeServerNameMethod();
        urlClient.invokeHelloMethod();
    }

    private void invokeServerNameMethod() throws Exception {
        String parseURL = "http://localhost:9080/1_0_simple_service/";
        invokeServiceMethodAndReadReturnValue(parseURL);
    }

    private void invokeHelloMethod() throws Exception {
        String parseURL = "http://localhost:9080/1_0_simple_service/user";
        invokeServiceMethodAndReadReturnValue(parseURL);
    }

    private void invokeServiceMethodAndReadReturnValue(String parseURL)
            throws Exception {
        Client client = Client.create();
        WebResource webResource = client.resource(parseURL);
        String response = webResource.get(String.class);
        System.out.println(response);
    }

}

pom.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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.training.rs</groupId>
    <artifactId>jax_rs_clients</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.18.3</version>
        </dependency>
    </dependencies>

</project>