我使用 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 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")