JAX-RS:将响应发送到浏览器后执行操作

JAX-RS: performing an action after sending the response to the browser

我的 objective 是在 RestFul 服务 returns 响应后执行一个操作。

我有下面的方法,但不确定什么时候做这个动作,就像方法一样returns我无法控制。有什么想法吗?

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/somepath")
public javax.ws.rs.core.Response someMethod (final RequestObject req) {

    // some actions

    return javax.ws.rs.core.Response.status(200).entity("response").build();

   //  here I would like to perform an action after the response is sent to the browser
}

你不能。 Java 这样不行。

只需触发一个 @Asynchronous 服务调用。它会立即 "fire and forget" 一个单独的线程。

@EJB
private SomeService someService;

@POST
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@Path("/somepath")
public Response someMethod(RequestObject request) {
    // ...

    someService.someAsyncMethod();
    return Response.status(200).entity("response").build();
}
@Stateless
public class SomeService {

    @Asynchronous
    public void someAsyncMethod() { 
        // ...
    }

}

另一种方法是 servlet 过滤器。