4

我正在使用Jersey 2.4.1Spring 3.2.4Jetty 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 managed bean, filterTest, registered with 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;
  [...]
}

异常映射器

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")
4

1 回答 1

3

这是一个错误...

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

于 2013-11-14T19:32:11.330 回答