Integrating Jersey 2 and Spring with Java Based Configuration

后端 未结 4 631
日久生厌
日久生厌 2020-12-30 12:18

I am using Jersey 2.10 and jersey-spring3 and Spring 4. I want to achieve DI(basically services) in jersey resources as well as in other places and want to create Spring Bea

4条回答
  •  庸人自扰
    2020-12-30 13:01

    web-app:

    
        contextClass
        
          org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      
    
    
    
        contextConfigLocation
        xxx.xxx.configuration.ApplicationConfiguration
    
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        SpringApplication
        org.glassfish.jersey.servlet.ServletContainer
        
            jersey.config.server.provider.classnames
            xxx.xxx.controllers.HelloController
        
        1
    
    
    
        SpringApplication
        /*
    
    

    JavaBased Configuration:

    @Configuration
    public class ApplicationConfiguration {
      @Bean
      HelloService helloService () {
        return new HelloServiceImpl();
      }
    }
    

    and simple controller:

    @Component
    @Path("/helloController")
    public class HelloController {
    
      @Autowired
      @Qualifier("helloService")
      private HelloService helloService ;
    
    
       @GET
       @Path("/hello")
       public String hello() {
        helloService.service();
      }
    }
    

    for testing:

    localhost:8080/[AppName]/helloController/hello

    remember about excluding old Spring dependencies you may have some conflicts if you don't. You can do this same as on the example below or through DependencyManagement.

    
    
        
    
        
            org.glassfish.jersey.ext
            jersey-spring3
            2.11
            
                
                    spring-context
                    org.springframework
                
                
                    spring-beans
                    org.springframework
                
                
                    spring-core
                    org.springframework
                
                
                    spring-web
                    org.springframework
                
                
                    jersey-server
                    org.glassfish.jersey.core
                
                
                    
                        jersey-container-servlet-core
                    
                    org.glassfish.jersey.containers
                
                
                    hk2
                    org.glassfish.hk2
                
            
        
    
        
        
            org.springframework
            spring-core
            4.0.6.RELEASE
        
    
        
            org.springframework
            spring-context
            4.0.6.RELEASE
        
    
        
            org.springframework
            spring-beans
            4.0.6.RELEASE
        
    
        
            org.springframework
            spring-web
            4.0.6.RELEASE
        
    
        
            org.springframework
            spring-aspects
            4.0.6.RELEASE
        
    
    
    

提交回复
热议问题