RESTful WS using Jersey Part 3 – Java Client

This post is with reference of Restful WS using Jersey Part 2 – Parameterized approach to provide a java client for accessing Restful services

The Complete code for Java Client

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package demo.client;

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

/**
*
* @author Yogesh
*/
public class JavaClient {

    public static void main(String[] args) {
       
String restUrlStr = "http://localhost:17286/HelloWorldRestWSPrj/resources/hello/Yogesh?query=age";
        URLConnection conn = null;
        try {
            URL restUrl = new URL(restUrlStr);
            conn = restUrl.openConnection();

            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setAllowUserInteraction(true);

            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            System.out.println("Response From Service");
            String line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }
}

Output – [ provided that service is deployed and running ]

Response From Service
<html><body><h1>Yogesh's age is 26</h1></body></html>

Comments

Popular posts from this blog

Composite Design Pattern by example

State Design Pattern by Example

Eclipse command framework core expression: Property tester