Building Simple Jersey Web App With Maven

I have recently configured our Hudson server to push latest Jersey snapshots to maven repository at https://maven2-repository.dev.java.net/.
Thanks to Martin Grotzke for his kind help!

I am pretty new to maven, and wanted to make sure Jersey related jars are deployed just fine and could be easily used. Following is steps to create a new Jersey based web application from scratch.
I will be using only CLI and maven.

Create a New Project Layout


At the beginning we need to create a new web project:

mvn archetype:create -DgroupId=my.example.com -DartifactId=SimpleRESTApp \\
-DarchetypeArtifactId=maven-archetype-webapp

Which should print out something like:


[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO] task-segment: [archetype:create] (aggregator-style)
[INFO] ------------------------------------------------------------------------
Downloading: http://repo1.maven.org/maven2/my/example/com/wagon-http-shared/1.0-beta-2/wagon-http-shared-1.0-beta-2.pom
Downloading: http://repo1.maven.org/maven2/my/example/com/wagon-http-shared/1.0-beta-2/wagon-http-shared-1.0-beta-2.pom
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:create]
[INFO] Defaulting package to group ID: my.example.com
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: maven-archetype-webapp:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: my.example.com
[INFO] Parameter: packageName, Value: my.example.com
[INFO] Parameter: basedir, Value: /export/home/japod/workspace/cli-jersey-app
[INFO] Parameter: package, Value: my.example.com
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: artifactId, Value: SimpleRESTApp
[INFO] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* End of debug info from resources from generated POM \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
[INFO] OldArchetype created in dir: /export/home/japod/workspace/cli-jersey-app/SimpleRESTApp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds

And create the following subtree:


SimpleRESTApp
|-src
|---main
|-----resources
|-----webapp
|-------WEB-INF

Creating a REST Resource Class


You will need to add at least one resource class.


cd SimpleRESTApp
mkdir -p src/main/java/com/example/my
vim src/main/java/com/example/my/SimpleResource.java

It could have the following content:


package com.example.my;
import javax.ws.rs.GET;
import javax.ws.rs.ProduceMime;
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 @ProduceMime("text/plain")
public String getGreeting() {
return "Hi there";
}
}

Adding Necessary Dependencies Into Our POM

Then we will need to introduce some dependencies at our POM file,
it could be done like follows:


<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</artifactId>
<version>0.8-ea-SNAPSHOT</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>

We have added a new repository, tomcat plugin and jersey dependency.

Configuring Jersey Servlet Adapter

Now the last step before we can actually run the web application is to properly
configure Jersey container. I will be using a servlet adapter and the configuration
needs to be placed in our web.xml file:


vim src/main/webapp/WEB-INF/web.xml

and add the following red lines there:


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

Running The Application


Now to get the application running, you can simply type:

mvn tomcat:run

You should see something like:


[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'tomcat'.
[INFO] ------------------------------------------------------------------------
[INFO] Building SimpleRESTApp Maven Webapp
[INFO] task-segment: [tomcat:run]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing tomcat:run
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to /export/home/japod/workspace/cli-jersey-app/SimpleRESTApp/target/classes
[INFO] [tomcat:run]
[INFO] Creating Tomcat server configuration at /export/home/japod/workspace/cli-jersey-app/SimpleRESTApp/target/tomcat
[INFO] Starting tomcat server
[INFO] Starting Servlet Engine: Apache Tomcat/5.5.15
[INFO] XML validation disabled
Jun 2, 2008 3:47:17 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
com.example.my
Jun 2, 2008 3:47:17 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Root resource classes found:
class com.example.my.SimpleResource
Jun 2, 2008 3:47:17 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Provider classes found:
Jun 2, 2008 3:47:18 PM com.sun.jersey.impl.wadl.WadlFactory createWadlResource
WARNING: WADL generation is disabled because JAXB jars are not included in the java class path. To enable WADL include JAXB 2.x jars in the java class path.
[INFO] Initializing Coyote HTTP/1.1 on http-8080
[INFO] Starting Coyote HTTP/1.1 on http-8080

Testing Your REST Resource


To check your resource works, you can try:

% curl -i http://localhost:8080/SimpleRESTApp/greeting
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/plain
Transfer-Encoding: chunked
Date: Mon, 02 Jun 2008 13:49:18 GMT
Hi there

Comments:

A perfect sample. Thank you very much.

Just in case anyone has the same problem:

I used a different package name for my resource class. When I ran the application, Tomcat threw the following exception:
"The ResourceConfig instance does not contain any root resource classes".

To solve the problem, I updated web.xml to use the package of my resource class i.e.

<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.sample</param-value>
</init-param>

Posted by Patrick Kimber on June 03, 2008 at 04:50 AM CEST #

The green on black is soooo easy to read.....

Posted by Doug Grove on June 05, 2008 at 10:18 AM CEST #

I don't know how I would have gotten the servlet adaptor configured without this -- maybe it should be in the docs?

Posted by Paul Snyder on August 08, 2008 at 02:52 PM CEST #

Can you do another one with ant? :D This was SO helpful. Thank the UNIX GODS for you. I <3 you.

Posted by blackHat on September 24, 2008 at 01:44 PM CEST #

sd

Posted by guest on July 24, 2009 at 03:39 PM CEST #

Comment

Posted by guest on February 19, 2010 at 03:32 AM CET #

i go thru given steps
but i get following errors
Missing:
----------
1) com.sun.jersey:jersey:jar:0.8-ea-SNAPSHOT

Posted by Praful on February 24, 2010 at 12:46 AM CET #

This is clearly replica watches the job for our legal fraternity to engage the establishment to necessary breitling watches steps by filing petitions in various courts. IF one fails another should be cartier watches filed taking every one to task. It is rolex watches useless to suggest ways and means to solve tag heuer watches the day to day problem to well paid employees tissot watches of government controlled establishments. Only active omega watches judiciary will resolve this problem.
http://www.watchvisa.com
http://www.watchvisa.com/breitling-watches.html
http://www.watchvisa.com/cartier-watches.html
http://www.watchvisa.com/movado-watches.html
http://www.watchvisa.com/omega-watches.html
http://www.watchvisa.com/rolex-watches.html
http://www.watchvisa.com/tag_heuer-watches.html
http://www.watchvisa.com/tissot-watches.html

Posted by jiangfan on February 26, 2010 at 11:43 PM CET #

When running on windows I had to change url-pattern from url-pattern "/\*"
to "/*" for it to work.

Posted by Rohit on October 21, 2011 at 03:38 AM CEST #

The Maven repository listed at the beginning of this post is no longer active. I found the the jars in another repository here:
https://maven.java.net/content/repositories/releases/

Posted by guest on July 21, 2012 at 05:33 AM CEST #

I had to change the url to /* in Ubuntu for it to work. Thanks Rohit.

Posted by Prathik on December 26, 2013 at 12:36 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Jakub Podlesak-Oracle

Search

Archives
« September 2015
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today