I am moving my spring web app from Spring 3.2 to 4.1.
In 3.2, the following exception handler works very well for catching exceptions from the entire applicaton.
@ControllerAdvice
public class RestExceptionProcessor {
@org.springframework.web.bind.annotation.ExceptionHandler(AppException.class)
@ResponseBody
public ErrorInfo handleAppException(AppException ex, HttpServletResponse response) {
ErrorInfo ret = new ErrorInfo(ex.getMessage(), new Date(), ex.getExtras());
logger.error(ex.getMessage());
response.setStatus(ex.getCode().getStatusCode());
return ret;
}
}
However when I move to 4.1, I get the following exception stack trace:
ERROR Failed to invoke @ExceptionHandler method: public com.momoe.handler.RestExceptionProcessor$ErrorInfo com.momoe.handler.RestExceptionProcessor.handleAppException(com.momoe.commons.AppException,javax.servlet.http.HttpServletResponse)
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:134) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:101) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:167) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:362) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE].....
How can I fix this?