我创建了一个拦截器,该拦截器拦截对方法的调用及其响应。发生的是,我必须在截取时使用服务,并且在请求被截取时该服务可以正常工作,但是在响应被截取时该服务为null。
我尝试分离拦截器,因此WriterInterceptor的实现在另一个类中,但其行为似乎相同。
@POST
@Path("/test")
@MyAnnotation
public void test(
final @Suspended AsyncResponse response,
final @ApiParam(value = "Test.", required = true) @FormParam("test") String test)
throws ServiceException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(new AsyncResponseCallable(response) {
@Override
public Response execute() throws Exception {
Object object = service.foo();
Status status = Status.OK;
return Response.status(status).entity(object).build();
}
});
}
@Provider
@MyAnnotation
public class MyInterceptor implements WriterInterceptor {
@EJB
private MyService myService;
@AroundInvoke
public Object interceptor(InvocationContext context) throws Exception {
myService.test();
return context.proceed();
}
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws WebApplicationException {
myService.test();
context.proceed();
}
}
@Stateless
public class MyService {
public void test() {
System.out.println("Done");
}
}
aroundWriteTo方法应具有由EJB注释注入的myService的实例,但它为null。 你们可以帮我解决这个问题吗?