Restful Java client using Apache HttpClient example

Example

Add below dependency in your maven pom.xml

    <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.4.1</version>
        </dependency>

Test class

package com.vinod.vinod_rest_examples;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class ApacheHttpRestClient {

    public static void main(String[] args) throws ClientProtocolException, IOException {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet("xxxxxx--service url");
        HttpResponse response = httpClient.execute(request);
        BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";
        while ((line = br.readLine()) != null) {
            System.out.println("Output: " + line);
        }
    }
}

How to create Web services using eclipse

Java Restful web services simple example - Pretech Blog

Web services using eclipse example

Java Restful web services simple example - Pretech Blog

JAX-RS Simple example

Java Restful web services simple example - Pretech Blog

Java Restful Get method example

Java Restful web services simple example - Pretech Blog

Java Web services simple example

Java Restful web services simple example - Pretech Blog

JAX-RS + Jersey + Tomcat example

Restful Web services 

Representational State Transfer
REST is an architectural style which is based on web-standards and the HTTP protocol.
see more details in Wikipedia
HTTP Methods
The PUT, GET, POST and DELETE methods are typical used in REST based architectures.
see more details in Wikipedia
Java Restful Implementations
See more details Wikipedia
Here is one simple example to get state details in an xml up on passing state code

Create a maven web-app project with below dependencies

 <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-jetty-http</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.7</version>
        </dependency>

Create a Controller class (Service implementation)

package com.vinod.webservice;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("/state")
public class StateController {
    @GET
    @Path("/{param}")
    @Produces("application/xml")
    public String getMsg(@PathParam("param") String state) {
       
        //if requried use JAXB API to convert our State object to xml
        String stateDetails = null;
        if (state.equals("KL")) {
            stateDetails = "<State><name>KERALA</name><shortname>KL</shortname>"
                    + "<headq>TRIVANDRUM</headq><language>MALAYALAM</language></State>";
        } else if (state.equals("KA")) {
            stateDetails = "<State><name>KARNATAKA</name><shortname>KA</shortname>"
                    + "<headq>BANGALORE</headq><language>KANNADA</language></State>";
        } else if (state.equals("TN")) {
            stateDetails = "<State><name>TAMILNADU</name><shortname>TN</shortname>"
                    + "<headq>CHENNAI</headq><language>TAMIL</language></State>";
        } else {
            stateDetails = "Data not found";
        }
        return stateDetails;

    }

}

Update web.xml

<servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.vinod.webservice</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/vinod/*</url-pattern>
    </servlet-mapping>
Deploy application in to tomcat server and run it
http://localhost:8080/vinod-webservices/vinod/state/KL
 
Download example
 
Done!!!!

Confusion Matrix + Precision/Recall (Super Simple, With Examples)

  Confusion Matrix + Precision/Recall (Super Simple, With Examples) 1) Binary Classification Setup Binary classification means the model p...

Featured Posts