Spring and Jersey Integration Example

This tutorial explains how to write a simple example using Spring and Jersey. This example uses Jersey for the REST implementation, Spring is used for managing the beans. In my previous tutorial, I have explained how to write a simple REST application using Jersey. We also can use Spring alone for implementing the RESTful web services.

1. Add Jersey and Spring JAR files

Look at the below picture for the list of JARs added for this application

jersey-spring-jars

2. Create a Spring Bean

package org.jersey.examples;

public class SpringBeanExample{
	public String getValue(){
		return "Spring and Jersey Integration";
	}
}

3. Create Jersey Service

package org.jersey.examples;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Path("/hello")
public class JerseySimpleExample {
	@Autowired
	SpringBeanExample springBean;

	@GET
	@Path("/javabeat")
	public Response getMessage() {
		String result = springBean.getValue();
		return Response.status(200).entity(result).build();
	}
}

4. Spring Configuration XML

<context:component-scan base-package="org.jersey.examples" />
<bean id=0"springBean" class="org.jersey.examples.SpringBean" />

5. Web.xml Configuration

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>Jersey Example</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>\WEB-INF\dispatcher-servlet.xml</param-value>
	</context-param>
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<servlet>
		<servlet-name>jersey</servlet-name>
		<servlet-class>
                     com.sun.jersey.spi.container.servlet.ServletContainer
        </servlet-class>
		<init-param>
		     <param-name>com.sun.jersey.spi.spring.container.servlet.SpringServlet</param-name>
		     <param-value>org.jersey.examples</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>jersey</servlet-name>
		<url-pattern>/jersey/*</url-pattern>
	</servlet-mapping>
</web-app>

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. Arilson says:

    Hello Krishna,

    I’d like to take a winsdow yours.
    I’m using Spring but I don’t know yet witch implementation to use.
    Jersey, CXF or any of both?

    Thanks,

    Arilson

  2. sherabc says:

Speak Your Mind

*