If I have a method such as the following in a Controller:
@RequestMapping(value = "/mymapping",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public final JsonResponse<?> myMethod() {
When the method throws an exception, I want a MVC interceptor to catch it and return a JSON error. I only want this to work for produces = MediaType.APPLICATION_JSON_VALUE
methods in the controller.
How can I do this in my afterCompletion
of HandlerInterceptor
. If I inspect the contentType of the response it is null when an exception occurs.
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
//TODO
}