Java 在JAX-RS中的每个@POST或@DELETE请求之前或之后调用方法

Java 在JAX-RS中的每个@POST或@DELETE请求之前或之后调用方法,java,web-services,rest,jax-rs,Java,Web Services,Rest,Jax Rs,JAX-RS中是否有任何方法或注释允许我在执行匹配请求方法之前或之后调用方法。假设我有以下服务类: public class MyService { ... @POST @Path("{id : \\d+}") @Consumes(MediaType.APPLICATION_JSON) public Response updateServiceObject(@PathParam("id") long id, InputStream is) {

JAX-RS中是否有任何方法或注释允许我在执行匹配请求方法之前或之后调用方法。假设我有以下服务类:

public class MyService {

    ...

    @POST
    @Path("{id : \\d+}")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response updateServiceObject(@PathParam("id") long id, InputStream is) {
        try {
            // Fetch the service object ...
            ServiceObject updatedServiceObj = readServiceObject(is);

            // ... and try to update it.
            updated = getServiceObjectDao().update(updatedServiceObj);

            if (updated == 0) {
                throw new WebApplicationException(Response.Status.NOT_FOUND);
            }

            return Response.ok().build();
        } catch (Exception e) {
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }
    }

    @DELETE
    @Path("{id : \\d+}")
    public Response deleteServiceObject(@PathParam("id") long id) {
        try {
            getServiceObjectDao().deleteById(id);
        } catch (Exception e) {
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }

        return Response.ok().build();
    }
}

我想添加一个方法
logEvent()
,它记录调用了哪个方法以及提供了哪些参数(仅@PathParam值)。因此它必须在每次调用之前或之后被调用。

使用面向方面编程(AOP)

这听起来非常适合于方面或过滤器。筛选器是HTTP方面。

将其添加到您的cxf xml中

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:cxf="http://cxf.apache.org/core"
      xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <cxf:bus>
        <cxf:features>
            <cxf:logging/>
        </cxf:features>
    </cxf:bus> 
</beans>


参考:

本页介绍了如何使用JAX-RS为CXF实现拦截器和过滤器。

您可以使用过滤器或拦截器。还有一些框架内置了对日志记录的支持。你的框架是什么?我使用的是ApacheCXF。你使用的是什么版本的JAX-RS?什么是AOP?你能解释一下或者给出与此相关的链接吗