如何在没有web.xml配置的情况下从@Configuration加载Jersey 2 / Spring 4加载bean

时间:2016-08-31 20:06:50

标签: java spring jersey jax-rs

为了学习(Spring Boot很棒且很神奇......),我正在尝试使用Jersey和Spring(用于DI)设置一个Web应用程序。我设法让它工作,但只有Spring xml配置。

我扩展了ResourceConfig

@ApplicationPath("/api")
public class JerseyWebApp extends ResourceConfig {

  public JerseyWebApp() {
      packages("com.company.controller");
  }
} 

我的HelloController.java

@Path("/hello")
public class HelloController {

  private HelloService helloService;

  @Autowired
  public HelloController(HelloService helloService) {
      this.helloService = helloService;
  }

  @GET
  public String hello() {
      return helloService.sayHello();
  }
}

我的HelloService.java

@Component
@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
public class HelloService {

  public String sayHello() {
    return "hello from jersey webapp";
  }
}

和我的spring applicationContext.xml

<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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="com.company" />
</beans>

还附上我的pom文件

<properties>
    <jersey2.version>2.23.2</jersey2.version>
    <jaxrs.version>2.0.1</jaxrs.version>
    <spring.version>4.3.2.RELEASE</spring.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey2.version}</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-spring3</artifactId>
        <version>${jersey2.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>spring-context</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-beans</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-core</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-web</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jersey-server</artifactId>
                <groupId>org.glassfish.jersey.core</groupId>
            </exclusion>
            <exclusion>
                <artifactId>
                    jersey-container-servlet-core
                </artifactId>
                <groupId>org.glassfish.jersey.containers</groupId>
            </exclusion>
            <exclusion>
                <artifactId>hk2</artifactId>
                <groupId>org.glassfish.hk2</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <!--Spring 4-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
    </dependency>
</dependencies>

是否可以让Jersey通过@Configuration类加载bean,或者我是否应该考虑使用Spring web / mvc来引导整个应用程序。

1 个答案:

答案 0 :(得分:2)

图书馆jersey-spring3用于&#34;某些原因&#34;默认情况下,文件org.glassfish.jersey.server.spring.SpringWebApplicationInitializer.java中正在查找类路径资源:

sc.setInitParameter("contextConfigLocation", "classpath:applicationContext.xml");

并且它不支持基于Java的Spring配置。这是一个已知未解决的问题here

因此,要使用Spring DI注册基于Java的注释配置,我必须解决它并使用ServletContainerInitializer的自定义实现来引导我的Web应用程序。

<强> JerseyServletContainerInitializer.java

public class JerseyServletContainerInitializer implements ServletContainerInitializer {

  @Override
  public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {

    //Register spring configurations
    AnnotationConfigWebApplicationContext springContext = new AnnotationConfigWebApplicationContext();
    springContext.register(SpringConfig.class, JPAConfig.class);
    servletContext.addListener(new RequestContextListener());
    servletContext.addListener(new ContextLoaderListener(springContext));
    springContext.setServletContext(servletContext);

    //Register Jersey servlet
    ServletRegistration.Dynamic servletRegistration = servletContext.addServlet("JerseyServlet", new ServletContainer());
    servletRegistration.addMapping("/api/*");
    servletRegistration.setLoadOnStartup(1);
    servletRegistration.setInitParameter("javax.ws.rs.Application", JerseyResourceConfig.class.getName());
  }
}

<强> JerseyResourceConfig.java

public class JerseyResourceConfig extends ResourceConfig {

  public JerseyResourceConfig() {
    packages("com.company.controller");
  }
}