Searching...
Sunday, 23 June 2013

JAX-RS + Jersey + Tomcat example

00:04

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!!!!

0 comments: