• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Spring and Jersey Integration Example

November 14, 2013 //  by Krishna Srinivasan//  Leave a Comment

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>

Category: Spring FrameworkTag: Jersey, Spring Integration, Spring REST

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.

Previous Post: « RESTful Web Service using Jersey
Next Post: java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact