Spring DI - Autowired property is null in a REST service

前端 未结 4 1213
攒了一身酷
攒了一身酷 2020-11-27 17:49

I\'m getting started with Spring DI, but I\'m struggling with dependency injection and the worse part is that I\'m not even sure why as it seems ok to me. Hopefully you guys

4条回答
  •  迷失自我
    2020-11-27 18:37

    Integration Spring with Jersey 2 (org.glassfish.*):

    Maven

    Some dependencies may be unnecessary, please check & clear it after things got working.

        
            2.5
        
    
        
        
            org.glassfish.jersey.core
            jersey-server
            ${jersey.version}
        
    
        
            org.glassfish.jersey.containers
            
            jersey-container-servlet
            ${jersey.version}
        
        
            org.glassfish.jersey.core
            jersey-client
            ${jersey.version}
        
        
            org.glassfish.jersey.test-framework.providers
            jersey-test-framework-provider-inmemory
            ${jersey.version}
        
    
        
        
            org.glassfish.jersey.ext
            jersey-spring3
            ${jersey.version}
            
                
                    org.springframework
                    spring
                
                
                    org.springframework
                    spring-core
                
                
                    org.springframework
                    spring-web
                
                
                    org.springframework
                    spring-beans
                
                
                    org.springframework
                    spring-context
                
            
        
    

    web.xml

    
        my-rest-service
        org.glassfish.jersey.servlet.ServletContainer
        
            jersey.config.server.provider.packages
            my.package.with.rest.services
        
        1
    
    
    
        my-rest-service
        /api/*
    
    

    applicationContext.xml

    During the Spring upgrading I had to move it from /main/webapp/WEB-INF/ to /main/resources/ (details).

    
    
    
        
        
    
    

    Example REST service

    public interface MyService
    {
        String work(String s);
    }
    
    ...
    
    @Service
    public class MyServiceImpl implements MyService
    {
        @Override
        public String work(String s)
        {
            return "Hello, " + s;
        }
    }
    
    ...
    
    @Path("demo/")
    @Component
    public class DemoRestService
    {
        @Autowired
        private MyService service;
    
        @GET
        @Path("test")
        public Response test(@FormParam("param") String par)
        {
            try
            {
                String entity = service.work(par);
                return Response.ok(entity).build();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Epic REST Failure").build();
            }
        }
    }
    

提交回复
热议问题