[STARTER] JAX-RS: Jersey + Servlet 3.0 + Maven + Tomcat

Steps to configure a JAX-RS project that runs on Tomcat (here 8), Maven, Servlet 3.0 and having Jersey as a Reference Implementation.

1. Maven POM.XML dependencies:


	<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.10.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-common</artifactId>
            <version>2.10.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.10.1</version>
        </dependency>

 

2. web.xml (src/main/webapp/WEB-INF)


<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</web-app>

 

3. Application class (under: src/main/java/com/vvirlan/java/ws

package com.vvirlan.java.ws;

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import com.vvirlan.java.ws.resources.Hello;

@ApplicationPath("/rest")
public class MyApplication extends Application {

	@Override
	public Set<Class<?>> getClasses() {
		final Set<Class<?>> returnValue = new HashSet<Class<?>>();
		returnValue.add(Hello.class);
		return returnValue;
	}
}

4. Resource class (which is the actual web service endpoint):

package com.vvirlan.java.ws.resources;

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

import javax.ws.rs.core.MediaType;

@Path("hello")
public class Hello {

	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String getIt() {
		return "Got it!";
	}

        @GET
	@Path("user/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public User getMe(@PathParam("id") String id) {
		User toReturn = new User();
		toReturn.setName(id);
		toReturn.setEmail("Email");
		return toReturn;
	}

}

5. Build the app as usually and deploy to tomcat webapp folder
6. Point the browser:
http://localhost:8080/demo2/rest/hello

Notes:
A. Folder layout:

screen

B. Complete 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>com.vvirlan.java.ws</groupId>
	<artifactId>demo2</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo2 Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.glassfish.jersey.core</groupId>
			<artifactId>jersey-server</artifactId>
			<version>2.10.1</version>
		</dependency>
		<dependency>
			<groupId>org.glassfish.jersey.core</groupId>
			<artifactId>jersey-common</artifactId>
			<version>2.10.1</version>
		</dependency>
		<dependency>
			<groupId>org.glassfish.jersey.containers</groupId>
			<artifactId>jersey-container-servlet</artifactId>
			<version>2.10.1</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>demo2</finalName>
	</build>
</project>

9 thoughts on “[STARTER] JAX-RS: Jersey + Servlet 3.0 + Maven + Tomcat

  1. For simple cases it is used by default (should be the same for all servers):
    “By default, WebLogic Server defines a default RESTful Web service application context path, resources. The default RESTful Web service application context path is used if the following are true:

    You did not update the web.xml deployment descriptor to include a Servlet mapping, as described in Packaging With a Servlet.

    The @ApplicationPath annotation is not defined in the javax.ws.rs.core.Application subclass, as described in Packaging With an Application Subclass.

    More info could be found here:
    https://docs.oracle.com/cd/E24329_01/web.1211/e24983/configure.htm#RESTF189

    Like

  2. […] 此处有更多详细信息: jax-rs的入门示例 […]

    Like

Leave a comment