Goal

In this article i will show you how to develop a JAX-RS RESTful web service. It uses the reference implementation Jersey and runs on tomcat. This article based on classes from Rest with jersey

Used technologies

JDK 1.8
Maven 3.2
Tomcat 8
Eclipse IDE (Luna)
Maven dependencies: jersey 1.19.4

1. create maven project in eclipse


2. pom.xml

Please change the pom file, the dependencies part should look like this
	    <dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-server</artifactId>
			<version>1.19.4</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-servlet</artifactId>
			<version>1.19.4</version>
		</dependency>
		<!-- to fix MessageException: ... and MIME media type application/json was not found. -->
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-bundle</artifactId>
			<version>1.19.4</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-jaxrs</artifactId>
			<version>1.9.13</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-xc</artifactId>
			<version>1.9.13</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

3. Set target runtime

For the first time you have to set the project targeted runtime (Tomcat), see the below image

4. Copy classes from previous arcticle

At first add a new package com.wstutorial.rest and copy the following classes from here
TestData.java
Tutorial.java
TutorialResource.java

In case if "src/main/java" does not exist
The "maven-archetype-webapp" is not creating "src/main/java" source folder by default, in this case you can add a new folder After this right-click on your project and then click Maven -> Update Project ...

5. Change web.xml

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  	<servlet>
		<servlet-name>jersey</servlet-name>
		<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>jersey</servlet-name>
		<url-pattern>/api/*</url-pattern>
	</servlet-mapping>
</web-app>

6. Run the application

right-click on your project, Run AS -> Run on Server, then select "Tomcat ...at Localhost" and click "finish"

7. Using curl

curl http://localhost:18081/jaxrs-jersey-tomcat/api/tutorials
curl -i curl http://localhost:18081/jaxrs-jersey-tomcat/api/tutorials/1
curl -H "Content-Type: application/json" -X POST -d '{"author":"Adam snake","id":"1","name":"Python Basics"}' http://localhost:18081/jaxrs-jersey-tomcat/api/tutorials
curl -H "Content-Type: application/json" -X PUT -d '{"author":"Adam changed","id":"1","name":"Python Basics"}' http://localhost:18081/jaxrs-jersey-tomcat/api/tutorials
curl -X DELETE http://localhost:18081/jaxrs-jersey-tomcat/api/tutorials
curl -X OPTIONS http://localhost:18081/jaxrs-jersey-tomcat/api

References

  • Jersey