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


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;

public class JerseySimpleExample {
	SpringBeanExample springBean;

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!