@ControllerAdvice注释不起作用

时间:2014-06-24 08:12:32

标签: spring spring-mvc

我知道这个问题已被提出,但提出的解决方案对我不起作用。在启动时,bean restErrorHandler被创建并且它的构造函数被调用。但是当我在控制器中抛出运行时异常时,它不会调用此错误处理程序。相反,它调用默认处理程序:

Type=DEBUG,Category=org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver,Thread="http-bio-8080"-exec-3,MDC=,Text=Resolving exception from handler [public org.springframework.http.ResponseEntity<com.egencia.auth.message.CreatePasswordPolicyResponse> com.egencia.service.controller.PasswordPolicyEndpoint.createPasswordPolicy(com.egencia.auth.message.CreatePasswordPolicyRequest,java.lang.String)]: java.lang.RuntimeException

ControllerAdvice。

@ControllerAdvice
public class RestErrorHandler extends ResponseEntityExceptionHandler {
private static final Logger LOGGER = LoggerFactory
        .getLogger(RestErrorHandler.class);




private Map<Class<? extends Exception>, HttpStatus> statuses;


public RestErrorHandler() {
    Map<Class<? extends Exception>, HttpStatus> map = new HashMap<Class<? extends Exception>, HttpStatus>();
    map.put(IllegalArgumentException.class, HttpStatus.BAD_REQUEST);
    map.put(UnsupportedOperationException.class, HttpStatus.BAD_REQUEST);
    map.put(BadSqlGrammarException.class, HttpStatus.BAD_REQUEST);
    map.put(DataIntegrityViolationException.class, HttpStatus.BAD_REQUEST);
    map.put(HttpMessageConversionException.class, HttpStatus.BAD_REQUEST);
    map.put(HttpMediaTypeException.class, HttpStatus.BAD_REQUEST);
    statuses = map;
}

/**
 * Map from exception type to Http status.
 * 
 * @param statuses
 *            the statuses to set
 */
public void setStatuses(Map<Class<? extends Exception>, HttpStatus> statuses) {
    this.statuses = statuses;
}


@ExceptionHandler({RuntimeException.class})
public  ResponseEntity<Object> processError(Exception ex, WebRequest request) {
    LOGGER.debug("inside rest error handler");



}

}

我也尝试过@EnableWebMVC注释。

1 个答案:

答案 0 :(得分:0)

尝试使用@Controller注释注释RestErrorHandler

相关问题