Announcement Announcement Module
Collapse
No announcement yet.
scopes defined for jersey and spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • scopes defined for jersey and spring

    I'm attempting to use an @Configuration in conjunction with Jersey's JAX-RS and I keep getting the following exception:

    Code:
    java.lang.RuntimeException: The scopes defined for jersey and spring do not match for the resource class com.capitalone.mobile.rest.SimpleRest and bean with name simpleRest!
    	at com.sun.jersey.spi.spring.container.servlet.SpringComponentProvider.getInstance(SpringComponentProvider.java:130)
    	at com.sun.jersey.spi.spring.container.servlet.SpringComponentProvider.getInstance(SpringComponentProvider.java:78)
    	at com.sun.jersey.impl.application.WebApplicationImpl$AdaptingResourceComponentProvider.getInstance(WebApplicationImpl.java:392)
    	at com.sun.jersey.impl.resource.PerRequestProvider.getInstance(PerRequestProvider.java:97)
    	at com.sun.jersey.impl.application.WebApplicationContext.getResource(WebApplicationContext.java:122)
    I can resolve this by making the classes Singleton's but I would prefer to not do that. Here is my config class:

    Code:
    @Configuration
    @ComponentScan(basePackages={"com.capitalone.mobile.rest"})
    public class MobileSpringConfig {
    	@Bean
    	public SimpleService simpleService() {
    		return new SimpleServiceImpl();
    	}
    
    	@Bean(name = "locale")
    	public Locale locale() {
    		return new Locale("en", "US");
    	}
    
    	@Bean
    	public ReloadableResourceBundleMessageSource messageSource() {
    		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    		messageSource.setBasename("messages");
    		return messageSource;
    	}
    }
    And my Jersey Service:

    Code:
    @Named
    @Path("/simple")
    public class SimpleRest {
    	@Inject
    	SimpleService simpleService;
    	
    	@GET
    	@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    	public SimpleResponse getSimpleResponse()
    	{
    		return simpleService.getSimpleResponse();
    	}
    }
    Thoughts on forcing PerRequest scope? I've tried using @Component with @Scope on the service but no luck.

  • #2
    You need to add another context listener

    org.springframework.web.context.request.RequestCon textListener

    .contextListenerClass(RequestContextListener.class )


    For more details see here:

    http://blogs.sun.com/enterprisetecht...rce/webnew.txt
    http://blogs.sun.com/enterprisetecht...sey_and_spring

    Comment


    • #3
      That still didn't quite work. So here is my web.xml:

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      	id="WebApp_ID" version="2.5">
      	<display-name>MobileWeb</display-name>
      	
      	<context-param>
      		<param-name>contextConfigLocation</param-name>
      		<param-value>/WEB-INF/applicationContext.xml</param-value>
      	</context-param>
      
      	<listener>
      		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      	</listener>
      	<listener>
      		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
      	</listener>
      
      	<servlet>
      		<servlet-name>MobileWeb</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.test.mobile.rest</param-value>
      		</init-param>
      	</servlet>
      	<servlet-mapping>
      		<servlet-name>MobileWeb</servlet-name>
      		<url-pattern>/rest/*</url-pattern>
      	</servlet-mapping>
      </web-app>
      And spring context:

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
      	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
      	xmlns:context="http://www.springframework.org/schema/context"
      	xsi:schemaLocation="
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
                        http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd">
      
      	<!-- component scan for EPF  @Configuration -->
      	<context:component-scan base-package="com.test.epf"
      		use-default-filters="false">
      		<context:include-filter type="annotation"
      			expression="org.springframework.context.annotation.Configuration" />
      	</context:component-scan>
      
      	<context:component-scan base-package="com.test.mobile.springconfig"
      		use-default-filters="false">
      		<context:include-filter type="annotation"
      			expression="org.springframework.context.annotation.Configuration" />
      	</context:component-scan>
      
      </beans>
      And here is my updated service:

      Code:
      @Path("/simple")
      @Log
      @Profile
      @Component
      @Scope("request")
      public class SimpleRest {
      	@Inject
      	SimpleService simpleService;
      	
      	@GET
      	@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
      	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
      	public SimpleResponse getSimpleResponse()
      	{
      		return simpleService.getSimpleResponse();
      	}
      }

      Comment


      • #4
        I got it working by looking at one of the responses to your post to use the com.sun.jersey.api.spring.Autowire annotation at the Jersey class level. Here is the updated code:

        Code:
        @Path("/simple")
        @Log
        @Profile
        @Autowire
        public class SimpleRest {
        	@Inject
        	SimpleService simpleService;
        	
        	@GET
        	@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
        	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
        	public SimpleResponse getSimpleResponse()
        	{
        		return simpleService.getSimpleResponse();
        	}
        }

        Comment


        • #5
          FYI - I was banging my head against the wall w/the same issue.

          My problem was not adding the RequestConextListener but wanting a PerRequest @Provider.

          I've got basically the same implementation going, but limiting the org.springframework annotations to javax.inject as much as possible.

          Anyone know if there is a JSR-friendly equiv to @Scope("request")?

          Comment

          Working...
          X