Reputation: 1216
I'd like to intercept every method call for methods that are annotated with @HttpMethod
or some sub-annotation of that.
It isn't convenient for me to create an annotation, that I put on every method that can be invoked by Jax-RS, so I turned to WriterInterceptor
and ReaderInterceptor
.
However, these aren't what I'm looking for, since I'd like it to intercept the method invocation, instead of the reading/writing process.
Filters are not good enough since I don't get to catch exceptions thrown by the method.
The first solution (plain java-ee interceptor) would be the best option if I didn't have to annotate every method with an arbitrary annotation.
What other options do I have?
Upvotes: 0
Views: 657
Reputation: 17435
This may be container specific but on at least Wildfly 18 I think I can do what you want. I'm using a servlet filter and pure JAX-RS - nothing RestEasy specific (nor Spring). My Application
code does:
@ApplicationPath("/rest")
public class RestApplicationConfig extends Application {
// intentionally empty
}
My Filter is:
@WebFilter(urlPatterns = "/rest/*")
public class FilterTest implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
try {
filterChain.doFilter(servletRequest, servletResponse);
}
catch( Throwable t ) {
// handle exception
}
}
}
Note that I do a try/catch around the doFilter
call. This is where you can catch any exceptions. An interesting addition is that I have a ContainerRequestFilter
also:
@Provider
@Priority(Priorities.AUTHENTICATION)
@PreMatching
public class AuthContainerRequestFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
}
}
and the catch
in the Filter is not called until this code runs in Wildfly. That makes sense as this is the JAX-RS part.
My "service" is just:
@Path("/v1/exception")
public class ExceptionService {
@Produces({ MediaType.TEXT_PLAIN })
@GET
public Response getException() {
throw new InternalError("this is an internal error");
}
}
Upvotes: 1