arahant
arahant

Reputation: 2203

spring 4 mvc app - application level exception handler

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?

Upvotes: 2

Views: 2365

Answers (1)

Master Slave
Master Slave

Reputation: 28569

When upgrading from spring 3.x to 4.1 you need to change from jackson 1.9.x to jackson 2.x. In jackson 2.x the package structure got migrated from org.codehaus to com.fasterxml.

If you don't have com.fasterxml jackson on your classpath the result will be the same as if you're missing the jackson altogether, outputting the Could not find acceptable representation message in logs. If you're using maven it should be enough to include jackson-databind, it will transitively include jackson-annotation and jackson-core

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.1</version>
</dependency>

a bit more details available here Spring 4 RestController JSON: characteristics not acceptable according to the request "accept" headers

Upvotes: 1

Related Questions