Jersey + Spring integration example

This tutorial show you how to integrate Jersey web application with Spring framework.

Technologies used :

  1. Jersey 1.8
  2. Spring 3.0.5.RELEASE
  3. Eclipse 3.6
  4. Maven 3

1. Project Dependency

Declares Jersey 1.8, Spring3 and “jersey-spring.jar” dependencies in Maven pom.xml file.

Note
In “jersey-spring.jar” version, it will download all the Spring 2.5.6 dependencies. To use Spring 3, you need to exclude those old Spring libraries manually.

	<repositories>
		<repository>
			<id>maven2-repository.java.net</id>
			<name>Java.net Repository for Maven</name>
			<url>http://download.java.net/maven/2/</url>
		</repository>
	</repositories>

	<dependencies>

		<!-- Jersey -->
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-server</artifactId>
			<version>1.8</version>
		</dependency>

		<!-- Spring 3 dependencies -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.0.5.RELEASE</version>
		</dependency>

		<!-- Jersey + Spring -->
		<dependency>
			<groupId>com.sun.jersey.contribs</groupId>
			<artifactId>jersey-spring</artifactId>
			<version>1.8</version>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-core</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-web</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-beans</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-context</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	
	</dependencies>

2. Spring Bean

A simple “transactionBo” bean is registered in Spring Ioc container. Later you will inject this bean into Jersey service.


package com.mkyong.transaction;
 
public interface TransactionBo{
 
	String save();
 
}

package com.mkyong.transaction.impl;

import com.mkyong.transaction.TransactionBo;

public class TransactionBoImpl implements TransactionBo {

	public String save() {

		return "Jersey + Spring example";

	}

}

File : applicationContext.xml – Register bean and enable the component auto scanning feature.


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
	<context:component-scan base-package="com.mkyong.rest" />
	
        <bean id="transactionBo" 
                  class="com.mkyong.transaction.impl.TransactionBoImpl" />
 
</beans>

3. Jersey

In REST method, you can to auto inject the “transactionBo” bean from Spring into Jersey.


package com.mkyong.rest;

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;
import com.mkyong.transaction.TransactionBo;

@Component
@Path("/payment")
public class PaymentService {

	@Autowired
	TransactionBo transactionBo;

	@GET
	@Path("/mkyong")
	public Response savePayment() {

		String result = transactionBo.save();

		return Response.status(200).entity(result).build();

	}

}

4. Integrate Jersey with Spring

The core integration is in web.xml

  1. Register Spring “ContextLoaderListener” listener class
  2. Change Jersey servlet from “com.sun.jersey.spi.container.servlet.ServletContainer” to “com.sun.jersey.spi.spring.container.servlet.SpringServlet“.

File : web.xml


<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>Restful Web Application</display-name>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

	<servlet>
		<servlet-name>jersey-serlvet</servlet-name>
		<servlet-class>
			com.sun.jersey.spi.spring.container.servlet.SpringServlet
		</servlet-class>
		<init-param>
			<param-name>
                                 com.sun.jersey.config.property.packages
                        </param-name>
			<param-value>com.mkyong.rest</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>jersey-serlvet</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>

</web-app>

5. Demo

jersey spring demo

Download Source Code

Download it – Jersey-Spring-Integration-Example.zip (8 KB)

References

  1. Jersey Spring JavaDoc
  2. Spring 3 in Jersey
  3. Spring auto component scanning
  4. RESTEasy + Spring integration example

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
47 Comment threads
20 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
53 Comment authors
Mayank jainGYANARANJAN PATRASteve TorresvahdetBen Dol Recent comment authors
newest oldest most voted
Rhea
Guest
Rhea

I was getting the below exception…

[org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/TAPP2]] (MSC service thread 1-3) StandardWrapper.Throwable: com.sun.jersey.core.spi.scanning.ScannerException: The URI scheme vfs of the URI vfs:/H:/Development/OffshoreDownloads/jboss-as-7.1.1.Final/jboss-as-7.1.1.Final/standalone/deployments/TAPP2.war/WEB-INF/classes/com/cablevision/tapp2/controller/ is not supported. Package scanning deployment is not supported for such URIs.

Try using a different deployment mechanism such as explicitly declaring root resource and provider classes using an extension of javax.ws.rs.core.Application

at com.sun.jersey.core.spi.scanning.PackageNamesScanner.scan(PackageNamesScanner.java:225) [jersey-core-1.8.jar:1.8]

at com.sun.jersey.core.spi.scanning.PackageNamesScanner.scan(PackageNamesScanner.java:139) [jersey-core-1.8.jar:1.8]

at com.sun.jersey.api.core.ScanningResourceConfig.init(ScanningResourceConfig.java:80) [jersey-server-1.8.jar:1.8]

at com.sun.jersey.api.core.PackagesResourceConfig.init(PackagesResourceConfig.java:104) [jersey-server-1.8.jar:1.8]

at com.sun.jersey.api.core.PackagesResourceConfig.(PackagesResourceConfig.java:78) [jersey-server-1.8.jar:1.8]

at com.sun.jersey.api.core.PackagesResourceConfig.(PackagesResourceConfig.java:89) [jersey-server-1.8.jar:1.8]

at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:700) [jersey-server-1.8.jar:1.8]

at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(WebComponent.java:678) [jersey-server-1.8.jar:1.8]

at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:203) [jersey-server-1.8.jar:1.8]

at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373) [jersey-server-1.8.jar:1.8]

at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556) [jersey-server-1.8.jar:1.8]

at javax.servlet.GenericServlet.init(GenericServlet.java:242) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]

at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1202) [jbossweb-7.0.13.Final.jar:]

at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1102) [jbossweb-7.0.13.Final.jar:]

at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3655) [jbossweb-7.0.13.Final.jar:]

at org.apache.catalina.core.StandardContext.start(StandardContext.java:3873) [jbossweb-7.0.13.Final.jar:]

at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]

at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)

at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_79]

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_79]

at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_79]

Felix
Guest
Felix

Hi, thx for tutorial. I have to fix my POM.xml with this.. <exclusion> <artifactId>spring-aop</artifactId> <groupId>org.springframework</groupId> </exclusion>

Steve Torres
Guest
Steve Torres

Hi.
This tutorial helped me a lot. I want to expand my application further by adding the MVC Interceptors into it. Any ideas? I have in my web.xml

Jersey Web Application
com.sun.jersey.spi.spring.container.servlet.SpringServlet

contextConfigLocation
/WEB-INF/spring/servlet.xml

1

Jersey Web Application
/rest/*

in my servlet.xml:

I have the interceptor and Manager already built. Please share your inputs.

vahdet
Guest
vahdet

How to apply Jersey ContainerRequestFilter and ContainerResponseFilter here?
If I define them as follows, the filters are ignored.

.
.

com.sun.jersey.spi.container.ContainerRequestFilters
com.isbank.sdf.rest.SdfRestHeaderFilter

com.sun.jersey.spi.container.ContainerResponseFilter
com.isbank.sdf.rest.SdfRestHeaderFilter

Great tutorial btw :) .

Naman Gala
Guest
Naman Gala

Hello mkyong

Thanks for the post. It is been so long that you have written this post, but now also it is useful to us.

If I make entry in my Web.xml for “com.sun.jersey.spi.spring.container.servlet.SpringServlet”

How to make this Rest service stateless? Or this example will expose stateless service only?

Note: I have to integrate this in existing Web application. and I have entry of

3600

in my web.xml.

Johaness
Guest
Johaness

as usual another broken sample its not works!

chitransu
Guest
chitransu

while running the above mentioned project i am getting below exception.. please help

May 05, 2015 9:13:43 PM org.apache.catalina.core.StandardContext loadOnStartup

SEVERE: Servlet /RESTfulExample threw load() exception

java.lang.ClassNotFoundException: org.springframework.aop.support.AopUtils

at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)

at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)

at com.sun.jersey.spi.spring.container.SpringComponentProviderFactory.getInjectableInstance(SpringComponentProviderFactory.java:240)

at com.sun.jersey.spi.spring.container.SpringComponentProviderFactory.access$100(SpringComponentProviderFactory.java:81)

at com.sun.jersey.spi.spring.container.SpringComponentProviderFactory$SpringManagedComponentProvider.getInjectableInstance(SpringComponentProviderFactory.java:230)

at com.sun.jersey.server.impl.component.IoCResourceFactory$SingletonWrapper.init(IoCResourceFactory.java:179)

at com.sun.jersey.server.impl.application.WebApplicationImpl$10.f(WebApplicationImpl.java:584)

at com.sun.jersey.server.impl.application.WebApplicationImpl$10.f(WebApplicationImpl.java:581)

at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)

at com.sun.jersey.server.impl.application.WebApplicationImpl.getResourceComponentProvider(WebApplicationImpl.java:581)

at com.sun.jersey.server.impl.application.WebApplicationImpl.initiateResource(WebApplicationImpl.java:658)

at com.sun.jersey.server.impl.application.WebApplicationImpl.initiateResource(WebApplicationImpl.java:653)

at com.sun.jersey.server.impl.application.RootResourceUriRules.(RootResourceUriRules.java:124)

at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1298)

at com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:169)

at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:775)

at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:771)

at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)

at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771)

at com.sun.jersey.spi.spring.container.servlet.SpringServlet.initiate(SpringServlet.java:117)

at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)

at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)

at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)

at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)

at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)

at javax.servlet.GenericServlet.init(GenericServlet.java:158)

at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)

at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)

at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)

at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5231)

at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5518)

at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)

at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)

at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)

at java.util.concurrent.FutureTask.run(FutureTask.java:166)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)

at java.lang.Thread.run(Thread.java:722)

shodhan
Guest
shodhan

did you get this error resolved? i am having the same issue

Guest
Guest
Guest

No tests?

deepak
Guest
deepak

Very helpful

Yair Zaslavsky
Guest
Yair Zaslavsky

Hi Mkyong, I have tested your code on tomcat8 + spring 4.0.5.RELEASE – besides the need to change the schemas at applicationContext.xml , everything works great. Wanted to share this piece of information, as you code refers to Spring 3.x

ChiragL
Guest
ChiragL

Hi Yair Zaslavsky,

I am trying integrate the same code with Spring4.0.5 and JPA, It would be really helpfull if you can share your code @ chirag.loliyana@gmail.com

Yair Zaslavsky
Guest
Yair Zaslavsky

Not sure I have this code anymore, can you please let me know what issues did you see? did you change the schema to spring 4.0.5 as I suggested 9 months ago? (wow, time flies)

Ben Dol
Guest
Ben Dol

What changes are required?

Yair Zaslavsky
Guest
Yair Zaslavsky

You should use relevant schemas of Spring, i.e – Spring 4.x schemas.

dubet
Guest
dubet

It worked for me.. thanks a lot for this useful information. God bless you.

Shinto Philip
Guest
Shinto Philip

Thank u so much… It helped me a lot…. :-)

Linnea Eng
Guest
Linnea Eng

Thanks for this great tutorial, yet again your knowledge saves the day! Keep up the good work :)

trackback
JAX-RS TUTORIALS | BUYWORKSELL

[…] Jersey + Spring integration example Integrate Jersey with Spring framework. […]

Karthik
Guest
Karthik

Hi…After implementing jersey with spring my application I am getting a null pointer exception for autowired class

Nov 03, 2013 11:58:03 AM org.glassfish.jersey.server.ServerRuntime$Responder mapException
WARNING: WebApplicationException cause:
java.lang.NullPointerException
at com.hyd.telos.MyResource.getIt(MyResource.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:151)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:152)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:367)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:349)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:106)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:259)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:983)
at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:334)
at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:209)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:722)

YoYo
Guest
YoYo

Very clear and simple article. Thanks!

I am also getting an NPE

[2014-01-05T19:30:30.635-0600] [glassfish 4.0] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=109 _ThreadName=http-listener-1(5)] [timeMillis: 1388971830635] [levelValue: 900] [[
StandardWrapperValve[jersey-serlvet]: Servlet.service() for servlet jersey-serlvet threw exception
java.lang.NullPointerException
at com.mkyong.rest.PaymentService.savePayment(PaymentService.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)

Any idea what’s up?

basavaraj
Guest
basavaraj

Hi, i am using the same above application for testing like how to create jersey webservice but i am getting below error SEVERE: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/JerseyWebservice]] at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source) at java.util.concurrent.FutureTask.get(Unknown Source) at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123) at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/JerseyWebservice]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) … 7 more Caused by: java.lang.NoClassDefFoundError: com/sun/jersey/spi/service/ComponentProvider at java.lang.Class.getDeclaredFields0(Native Method) at java.lang.Class.privateGetDeclaredFields(Unknown Source) at java.lang.Class.getDeclaredFields(Unknown Source) at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:106) at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:261) at org.apache.catalina.startup.WebAnnotationSet.loadApplicationServletAnnotations(WebAnnotationSet.java:140) at… Read more »

Dave
Guest
Dave

Hi,

I’m trying to get this working only without using xml configuration. I’m using Jersey/Tomcat/Spring/Maven. That being said, I’m having a problem getting my autowired attribute. If you have the chance, could you take a look?

Here are the full details:

http://stackoverflow.com/questions/19302445/unable-to-get-autowired-to-work-or-do-a-component-scan

Abhi
Guest
Abhi

Inject is not working for above code

Raghunath
Guest
Raghunath

Hello Yong Sir,

We are very much interested in your applications and thanks so much for the same.

Raghunath.

Anonymous
Guest
Anonymous

need another exclusion (spring-aop)

Arun
Guest
Arun

Hi, Everything works fine for me except loading the values from property file for SpringContextLoaderListener. I am getting the below the exception <User defined listener org.jboss.resteasy.plugins.spring.SpringContextLoaderListener failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dropDownDataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Cannot instantiate class: ${datasource.java.naming.factory.initial} [Root exception is java.lang.ClassNotFoundException: ${datasource.java.naming.factory.initial}]. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dropDownDataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Cannot instantiate class: ${datasource.java.naming.factory.initial} [Root exception is java.lang.ClassNotFoundException: ${datasource.java.naming.factory.initial}] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) Truncated. see log… Read more »

Pradip Bhatt
Guest
Pradip Bhatt

Hello mkyong..

Thanks for this post.
I use struts2 + spring3 + hibernate for my application. I have implementing Jersey successfully. Now whenever i call the service at that time following error is occured.

[LazyInitializationException:110] failed to lazily initialize a collection of role:
no session or session was closed

org.codehaus.jackson.map.JsonMappingException: failed to lazily initialize a collection of role: com.aspire.hrms.model.OrganizationGeneralInfo.users, no session or session was closed (through reference chain: java.util.ArrayList[0]->com.aspire.hrms.model.Department[“organization”]->com.aspire.hrms.model.OrganizationGeneralInfo[“users”])

Pratik Palkar
Guest
Pratik Palkar

Hi mkyong,

What is the use of the line ‘Response.status(200).entity(result).build()’ in above application?

Kiran
Guest
Kiran

Hi mkyong,
can I please suggest me how to integrate REST using Spring HessianServiceExporter

Lester
Guest
Lester

Hi Mr. Yong,

Could you give an example how to integrate Jersey test framework with this example. I am using this example with MYSQL, Spring JDBC and Tomcat. But I can not make unit test working with configuration problems. I use this library.

        <dependency>
			<groupId>com.sun.jersey.jersey-test-framework</groupId>
			<artifactId>jersey-test-framework-grizzly2</artifactId>
			<version>${jersey.version}</version>
			<scope>test</scope>
		</dependency>

Many thanks.

mahesh
Guest
mahesh

I am getting the error

 SEVERE: Context initialization failed
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.bnymellon.touchpoint.controller.TransactionBO] for bean with name 'BO' defined in ServletContext resource [/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: com.bnymellon.touchpoint.controller.TransactionBO
	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1250)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:576)
	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1319)
	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:885)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723)
	at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
	at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
	at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
	at java.util.concurrent.FutureTask.run(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.bnymellon.touchpoint.controller.TransactionBO
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
	at org.springframework.util.ClassUtils.forName(ClassUtils.java:257)
	at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:408)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1271)
	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1242)
	... 17 more

Jan 11, 2013 2:06:16 PM org.apache.catalina.core.StandardContext listenerStart

mahesh
Guest
mahesh

The bean isnot getting instanciated. Please let me know

kasav
Guest
kasav

Hi mkyong,

Will you please include a spring-jersey-jpa-hibernate blog on your site? I can’t find any working example on the internet.

ahmed
Guest
ahmed

hi i am using hersey + spring and JAXB annotation i have annotated all my model classes and the controler layer is my WS where i ve pouted WS annotation so i’ve got this messages ; GRAVE: A message body writer for Java class com.alpha.vconf.model.Reunion_$$_javassist_3, and Java type class com.alpha.vconf.model.Reunion, and MIME media type application/xml was not found 27 déc. 2012 14:19:27 com.sun.jersey.spi.container.ContainerResponse write GRAVE: The registered message body writers compatible with the MIME media type are: application/xml -> com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$App com.sun.jersey.core.impl.provider.entity.DocumentProvider com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$App com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$App */* -> com.sun.jersey.core.impl.provider.entity.FormProvider com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider com.sun.jersey.core.impl.provider.entity.StringProvider com.sun.jersey.core.impl.provider.entity.ByteArrayProvider com.sun.jersey.core.impl.provider.entity.FileProvider com.sun.jersey.core.impl.provider.entity.InputStreamProvider com.sun.jersey.core.impl.provider.entity.DataSourceProvider com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General com.sun.jersey.core.impl.provider.entity.ReaderProvider com.sun.jersey.core.impl.provider.entity.DocumentProvider com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General… Read more »

ahmed
Guest
ahmed

the problem is solved ; all my configuration is OK , it was problem related to the that i used to get an entity by id , instead of using get i have used load accidently

SRI
Guest
SRI

I followed the steps.I am using embedded jetty server and it’s not starting when i change servlet class name from
com.sun.jersey.spi.container.servlet.ServletContainer
to
com.sun.jersey.spi.spring.container.servlet.SpringServlet

giri
Guest
giri

sir, 1)we can use org.springframework.web.servlet.DispatcherServlet And com.sun.jersey.spi.spring.container.servlet.SpringServlet in a single Web.xml file ? 2)i am using both in a single web.xml file and some times it is working fine and some times not working the code. it is giving error like context initialization failed when i first start the application 3)My Web.xml file is <?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"> <welcome-file-list> <welcome-file>krams/main/persons/</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>spring1</servlet-name> <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> <!--<init-param> <param-name> com.sun.jersey.config.property.packages </param-name> <param-value>org.krams.tutorial.service</param-value> </init-param> --><load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring1</servlet-name> <url-pattern>/webser/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/krams/*</url-pattern> </servlet-mapping> </web-app>