Execute a resource method before each request

I have code in which one parameter (cookie) can be passed to any of the paths, and I want to process it in the same way:

@Path("/some/path")
public class JaxRsService {

    public void doStuff(@CookieParam("cookie") Cookie cookie) {
        handleCookie(cookie);
        // etc.
    }

    public void doStuff2(@CookieParam("cookie") Cookie cookie) {
        handleCookie(cookie);
        // etc.
    }

    public void doStuff3(@CookieParam("cookie") Cookie cookie) {
        handleCookie(cookie);
        // etc.
    }
}

Is there any way to account for this from each method? I tried to create a setter, but setters are only called at build time, so the cookie is not available.

@Path("/some/path")
public class JaxRsService {

    // This never gets called
    @CookieParam("cookie")
    public void setCookie(Cookie cookie) {
       cookie // stuff
    }

    // etc.
}

Similarly, there is an annotation @PostContruct, but it only works at build time.

Adding a cookie as a class variable works fine, but I still have to call the method in every request:

@Path("/some/path")
public class JaxRsService {

    // This never gets called
    @CookieParam("cookie")
    Cookie cookie;

    public void doStuff() {
        handleCookie();
        // etc.
    }

    public void doStuff2() {
        handleCookie();
        // etc.
    }

    public void doStuff3() {
        handleCookie();
        // etc.
    }
}

Is there a good way to handle this?

+3
source share
1 answer

In CXF, interceptors are the standard way to process a request. However, I do not know about the portable method.

... ( ) http://cxf.apache.org/docs/jax-rs-filters.html ( )

+1

All Articles