I have some code where one parameter (a cookie) can be passed to any of the paths, and I want to handle it 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 a way to factor this out of each method? I tried creating a setter, but setters only get called at construction time, so the cookie isn't available..
@Path("/some/path")
public class JaxRsService {
// This never gets called
@CookieParam("cookie")
public void setCookie(Cookie cookie) {
cookie // stuff
}
// etc.
}
Similarly, there's the @PostContruct
annotation, but it only works on construct time.
Adding the cookie as a class variable works fine, but I'd 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 any nice way to handle this?
In CXF the "interceptors" are the standard way of decorating a request. I don't know of a portable method, though.
... another method may be (proprietary, too) http://cxf.apache.org/docs/jax-rs-filters.html (which is nearly the same in essence)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.