2017-06-05 148 views
2

我在Spring Boot 1.5.3中有一个特殊的情况,在@ControllerAdvice中注释了ExceptionHandler。它捕获任何异常的默认异常,但如果我抛出一个自定义异常,它不会触发。Spring Boot @ControllerAdvice部分工作,不适用于自定义异常

的的ExceptionHandler:

@ControllerAdvice 
public class ResponseEntityExceptionHandler { 

@ExceptionHandler({ HttpMessageNotReadableException.class }) 
protected ResponseEntity<ErrorModel> handleInvalidJson(RuntimeException e, WebRequest request) { 
    return new ResponseEntity<ErrorModel>(new ErrorModel().message("Could not parse JSON."), HttpStatus.BAD_REQUEST); 
} 

@ExceptionHandler({ NumberFormatException.class }) 
protected ResponseEntity<ErrorModel> handleInvalidRequest(RuntimeException e, WebRequest request) { 
    return new ResponseEntity<ErrorModel>(new ErrorModel().message("Invalid request parameter."), HttpStatus.BAD_REQUEST); 
} 

@ExceptionHandler({ CannotCreateTransactionException.class }) 
protected ResponseEntity<ErrorModel> handleTransactionCreationException(RuntimeException e, WebRequest request) { 
    return new ResponseEntity<ErrorModel>(new ErrorModel().message("Error connecting to the database, please make sure it is still available."), HttpStatus.BAD_REQUEST); 
} 

@ExceptionHandler({ NotFoundException.class }) 
protected ResponseEntity<ErrorModel> handleApiException(RuntimeException e, WebRequest request) { 
    return new ResponseEntity<ErrorModel>(new ErrorModel().message(e.getMessage()), HttpStatus.NOT_FOUND); 
} 
} 

前3例外都得到捕获和处理,因为他们应该,但底部异常得到由默认的弹簧引导的ExceptionHandler处理。这是一个自定义异常,我抛出一个控制器内:

public ResponseEntity<?> deleteActor(@ApiParam(value = "Used to identify a single actor.", required = true) @PathVariable("actor_id") Integer actorId, @RequestHeader("Accept") String accept) throws Exception { 
Actor actor = actorRepository.findOne(actorId); 
if (actor == null) { 
    throw new NotFoundException(404, "Not found"); 
} 
actorRepository.delete(actorId); 
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT); 
} 

我试着抛顶例外像这样的一个:

public ResponseEntity<?> readActor(@ApiParam(value = "Used to identify a single actor.", required = true) @PathVariable("actor_id") Integer actorId, @RequestHeader("Accept") String accept) throws Exception { 
    Actor actor = actorRepository.findOne(actorId); 
    if (actor == null) { 
     throw new NumberFormatException(""); 
    } 
    return new ResponseEntity<Actor>(actor, HttpStatus.OK); 
} 

而这些得到处理就好了...

将tomcat日志也显示此:

2017-06-05 11:30:20.080 INFO 9076 --- [   main] .m.m.a.ExceptionHandlerExceptionResolver : Detected @ExceptionHandler methods in responseEntityExceptionHandler 

除外:

public class NotFoundException extends ApiException { 
private int code; 
public NotFoundException (int code, String msg) { 
    super(code, msg); 
    this.code = code; 
} 
} 

例外从这个基类继承:

public class ApiException extends Exception{ 
private int code; 
public ApiException (int code, String msg) { 
    super(msg); 
    this.code = code; 
} 
} 

为什么自定义异常由的ExceptionHandler避免检测任何想法?

如果有必要,我很乐意提供更多信息。

回答

1

对于这种特殊情况,答案是使用Exception而不是RuntimeException,因为NotFoundException只会从Exception继承。

另外值得注意的事项:

  • 捕获所有异常可以如果使用通用名例外,经常检查是否已导入正确的使用@ExceptionHandler(Exception.class)

+0

我添加了您的建议,由于某种原因,这会捕获前3个例外,但不是底部的例外。让一个函数捕获所有异常也不是期望的行为。我宁愿现在赶上特定的,所以我可以返回自定义错误消息。 – user6058309

+0

检查并导入正确的NotFoundException:/,我也尝试抛出并捕获更一般的ApiException,但没有成功。 – user6058309

+0

OK,我的最后一次尝试将使用异常,而不是RuntimeException的在handleApiException: 保护ResponseEntity handleApiException(例外五,WebRequest的请求){ 返回新ResponseEntity (新ErrorModel()消息(e.getMessage())。 ,HttpStatus.NOT_FOUND); – rainerhahnekamp

相关问题