java – Jersey&Spring托管提供商

我正在使用Jersey 2.4.1,Spring 3.2.4和Jetty 7.

我不明白Jersey如何注册由Spring管理的提供商.
我用@Provider注释的类也注释了@Component,因为我需要注入.
我将Jersey配置为使用这些提供程序扫描我的包.

以下是我配置Jersey servlet的方法:

public void createJerseyServlet() {
  ResourceConfig resourceConfig = new ResourceConfig();
  resourceConfig.packages("test.web", "org.glassfish.jersey.server.spring");
  resourceConfig.register(LoggingFilter.class);
  ServletHolder servletHolder = new ServletHolder(new ServletContainer(resourceConfig));
  servletContextHandler.addServlet(servletHolder, "/*");
}

启动时的日志:

INFO - org.glassfish.jersey.server.spring.SpringComponentProvider - Spring context lookup done.
INFO - org.glassfish.jersey.server.spring.SpringComponentProvider - Spring component provider initialized.
INFO - org.glassfish.jersey.server.spring.SpringComponentProvider - Spring managed bean, testResource, registered with HK2.
INFO - org.glassfish.jersey.server.spring.SpringComponentProvider - Spring managed bean, nullPointerExceptionMapper, registered with HK2.
INFO - org.glassfish.jersey.server.spring.SpringComponentProvider - Spring managed bean, filterTest, registered with HK2.
[...]
INFO - org.glassfish.jersey.server.ApplicationHandler - Jersey application initialized.
Root Resource Classes:
  org.obiba.jta.web.TestResource
Pre-match Filters:
   org.glassfish.jersey.filter.LoggingFilter
Global Response Filters:
   org.glassfish.jersey.filter.LoggingFilter
Global Reader Interceptors:
   org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor
Global Writer Interceptors:
   org.glassfish.jersey.filter.LoggingFilter
[...]

我的提供程序是通过Spring检测到的:Spring托管bean,filterTest,在HK2中注册但它们没有出现在ApplicationHandler日志中……
当我运行应用程序时,它们不会被使用.

我需要手动注册它们:

@Resource
private FilterTest filterTest;

@Resource
private MyExceptionMapper myExceptionMapper;

[...]

resourceConfig.register(filterTest);
resourceConfig.register(myExceptionMapper);

甚至更好……解决方法:(

Map<String, Object> providers = applicationContext.getBeansWithAnnotation(Provider.class);
for(Object provider : providers.values()) {
  resourceConfig.register(provider);
}

现在它可以工作(即使它们仍然没有出现在ApplicationHandler日志中)但是我不明白为什么Jersey在扫描包时没有自动注册它们并且看到这些bean是由Spring管理的…

这是我的组件……

资源

package test.web;

@Transactional
@Component
@Path("/test")
public class TestResource {
  @Resource
  private MyService myService;

  @GET
  @Path("/inject")
  @Produces(MediaType.TEXT_PLAIN)
  public String testInjection() {
    return "integrationService: " + integrationService;
  }

  @GET
  @Path("/active")
  @Produces(MediaType.TEXT_PLAIN)
  public String testActiveTransaction() {
    return "isActualTransactionActive: " + TransactionSynchronizationManager.isActualTransactionActive();
  }

  @GET
  @Path("/exception")
  @Produces(MediaType.TEXT_PLAIN)
  public String testException() {
    throw new MyException();
  }
}

过滤

package test.web.cfg;

@Provider
@Component
public class FilterTest implements ContainerRequestFilter {
  @Resource
  private MyService myService;
  [...]
}

ExceptionMapper

package test.web.cfg;

@Provider
@Component
public class MyExceptionMapper implements ExceptionMapper<MyException> {
  @Override
  public Response toResponse(MyException exception) {
    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("MyException was thrown!").build();
  }
}

最后我的依赖

compile("org.glassfish.jersey.core:jersey-common:2.4.1")
compile("org.glassfish.jersey.core:jersey-server:2.4.1")
compile("org.glassfish.jersey.containers:jersey-container-servlet-core:2.4.1")
compile("org.glassfish.jersey.media:jersey-media-multipart:2.4.1")
compile("org.glassfish.jersey.connectors:jersey-apache-connector:2.4.1")
compile("org.glassfish.jersey.ext:jersey-spring3:2.4.1")

解决方法:

这是一个错误……

程序包扫描组件注册表不起作用:https://java.net/jira/browse/JERSEY-2175

上一篇:java – 在Jersey 1.17中指定JAXB 2上下文


下一篇:java – 使用Jersey和Spring for REST服务的@Autowired属性的NullPointerException