January 5, 2013

Rest Example with Jersey (Reference Implementation of JAX-RS)

When reading the official Oracle J2EE 6 Tutorial there are very few complete and runnable example. So in this blog I will have a go with a simple Hello World REST example. REST is build on top web service and some people might right now be sceptical. But the REST architecture is said to deliver. The other problem with web service, is that is so many implementation, depending on which platform you are deploying to. In this blog I will use the reference implementation of JAX-RS, Jersey - http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html.

After that said, lets get started with our first runnable Hello World REST example with Jersey.

package se.msc.example.rest.jersey;

@javax.ws.rs.Path("/helloWorldRestJersey")
public class HelloWorldRestJersey {

    @javax.ws.rs.GET
    @javax.ws.rs.Path("/{param}")
    public javax.ws.rs.core.Response getMsg(
            @javax.ws.rs.PathParam("param") String msg) {

        String entity = "Rest Jersey response " + msg;
        return javax.ws.rs.core.Response.status(200).entity(entity).build();
    }
}

And here is the web.xml that contains the dispatcher servlet.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>se.msc.example.rest.jersey</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

And finally our pom.xml, that shows the jersey dependency and a maven jetty plugin, which we will use for running our example.

<?xml version="1.0" encoding="UTF-8"?>
<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>se.msc.examples</groupId>
    <artifactId>example-rest-jersey</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Rest Example with Jersey (Reference Implementation of JAX-RS)</name>
    <url>http://magnus-k-karlsson.blogspot.se/</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.outputEncoding>UTF-8</project.build.outputEncoding>
        <jetty.version>7.5.0.v20110901</jetty.version>
        <jersey.version>1.16</jersey.version>
    </properties>

    <dependencies>
        <!-- Jersey JAX-RS -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <!-- Test Support -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>

            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty.version}</version>
                <configuration>
                    <scanIntervalSeconds>1</scanIntervalSeconds>
                    <useTestClasspath>true</useTestClasspath>
                    <connectors>
                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                            <port>8080</port>
                            <maxIdleTime>3600000</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>com.sun.jersey</groupId>
                        <artifactId>jersey-core</artifactId>
                        <version>${jersey.version}</version>
                    </dependency>

                    <dependency>
                        <groupId>com.sun.jersey</groupId>
                        <artifactId>jersey-server</artifactId>
                        <version>${jersey.version}</version>
                    </dependency>

                    <dependency>
                        <groupId>com.sun.jersey</groupId>
                        <artifactId>jersey-servlet</artifactId>
                        <version>${jersey.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

Now lets test our application.

$ mvn jetty:run
Reference:

No comments: