Build Simple Restful Web App with Maven and Jersey

1) Create a New Project Layout
cd/d C:\tmp\restful
mvn archetype:create -DgroupId=my.example.com -DartifactId=SimpleRESTApp -DarchetypeArtifactId=maven-archetype-webapp
It will create the following subtree:
SimpleRESTApp
|-src
|---main
|-----resources
|-----webapp
|-------WEB-INF


2) Creating a REST Resource Class
package com.example.my;

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

// The Java class will be hosted at the URI path "/greeting"
@Path("greeting")
public class SimpleResource {

// and implement the following GET method
@GET
@Produces("text/plain")
public String getGreeting() {
return "Hi there";
}
}

3) Adding Necessary Dependencies Into Our POM,
C:\tmp\restful\SimpleRESTApp\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>my.example.com</groupId>
<artifactId>SimpleRESTApp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SimpleRESTApp Maven Webapp</name>
<url>http://maven.apache.org</url>

<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.1.0-ea</version>
</dependency>

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.1.0-ea</version>
</dependency>
<dependency>
<groupId>javax.ws</groupId>
<artifactId>jsr311</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.objectweb</groupId>
<artifactId>asm</artifactId>
<version>3.1</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
</plugin>

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>

<finalName>SimpleRESTApp</finalName>
</build>

</project>

4) Configuring Jersey Servlet Adapter
C:\tmp\restful\SimpleRESTApp\src\main\webapp\WEB-INF\web.xml

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

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

<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

<init-param>
<param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
<param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
</init-param>

<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.example.my</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>

5) Install jersey 1.1.0-ea maven repository

mvn install:install-file -DgroupId=com.sun.jersey -DartifactId=jersey-server -Dversion=1.1.0-ea -Dpackaging=jar -Dfile=E:\jersey-archive-1.1.0-ea\lib\jersey-server-1.1.0-ea.jar
mvn install:install-file -DgroupId=com.sun.jersey -DartifactId=jersey-core -Dversion=1.1.0-ea -Dpackaging=jar -Dfile=E:\jersey-archive-1.1.0-ea\lib\jersey-core-1.1.0-ea.jar
mvn install:install-file -DgroupId=javax.ws -DartifactId=jsr311 -Dversion=1.1 -Dpackaging=jar -Dfile=E:\jersey-archive-1.1.0-ea\lib\jsr311-api-1.1.jar
mvn install:install-file -DgroupId=org.objectweb -DartifactId=asm -Dversion=3.1 -Dpackaging=jar -Dfile=E:\jersey-archive-1.1.0-ea\lib\asm-3.1.jar

6) Running The Application
cd/C:\tmp\restful\SimpleRESTApp
mvn tomcat:run

7) Testing Your REST Resource
http://localhost:8080/SimpleRESTApp/greeting

Reference:
http://blogs.sun.com/japod/entry/building_simple_jersey_web_app
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章