我知道有人问过这个问题,但提出的解决方案对我不起作用。在启动时,创建了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.RuntimeExceptionControllerAdvice。
@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注解。
发布于 2015-05-22 11:52:20
尝试使用@ RestErrorHandler注解来注解控制器
https://stackoverflow.com/questions/24381514
复制相似问题