这篇教程主要专注于如何优雅的处理WEB中的异常。虽然我们可以手动的设置 ResponseStatus
,但是还有更加优雅的方式将这部分逻辑隔离开来。Spring提供了整个应用层面的异常处理的抽象,并且只是要求您添加一些注释 - 它会处理其他所有内容。下面是一些代码的示例
下面的代码中, DogController
将返回一个 ResponseEntity
实例,该实例中包含返回的数据和 HttpStatus
属性
List<Dog> DogsNotFoundException DogServiceException
@RestController
@RequestMapping("/dogs")
public class DogsController {
@Autowired private final DogsService service;
@GetMapping
public ResponseEntity<List<Dog>> getDogs() {
List<Dog> dogs;
try {
dogs = service.getDogs();
} catch (DogsServiceException ex) {
return new ResponseEntity<>(null, null, HttpStatus.INTERNAL_SERVER_ERROR);
} catch (DogsNotFoundException ex) {
return new ResponseEntity<>(null, null, HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(dogs, HttpStatus.OK);
}
}
这种处理异常的方式最大的问题就在于代码的重复。catch部分的代码在很多其它地方也会使用到(比如删除,更新等操作)
Spring提供了一种更好的解决方法,也就是Controller Advice。它将处理异常的代码在应用层面上集中管理。
现在我们的的DogsController的代码更加简单清晰了:
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.NOT_FOUND;
@ControllerAdvice
public class DogsServiceErrorAdvice {
@ExceptionHandler({RuntimeException.class})
public ResponseEntity<String> handleRunTimeException(RuntimeException e) {
return error(INTERNAL_SERVER_ERROR, e);
}
@ExceptionHandler({DogsNotFoundException.class})
public ResponseEntity<String> handleNotFoundException(DogsNotFoundException e) {
return error(NOT_FOUND, e);
}
@ExceptionHandler({DogsServiceException.class})
public ResponseEntity<String> handleDogsServiceException(DogsServiceException e){
return error(INTERNAL_SERVER_ERROR, e);
}
private ResponseEntity<String> error(HttpStatus status, Exception e) {
log.error("Exception : ", e);
return ResponseEntity.status(status).body(e.getMessage());
}
}
handleRunTimeException
:这个方法会处理所有的 RuntimeException
并返回 INTERNAL_SERVER_ERROR
状态码 handleNotFoundException
: 这个方法会处理 DogsNotFoundException
并返回 NOT_FOUND
状态码。 handleDogsServiceException
: 这个方法会处理 DogServiceException
并返回 INTERNAL_SERVER_ERROR
状态码
这种实现的关键就在于在代码中捕获需检查异常并将其作为 RuntimeException
抛出。
还可以用 @ResponseStatus
将异常映射成状态码
@ControllerAdvice
public class DogsServiceErrorAdvice {
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler({DogsNotFoundException.class})
public void handle(DogsNotFoundException e) {}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler({DogsServiceException.class, SQLException.class, NullPointerException.class})
public void handle() {}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({DogsServiceValidationException.class})
public void handle(DogsServiceValidationException e) {}
}
@ResponseStatus(HttpStatus.NOT_FOUND)
public class DogsNotFoundException extends RuntimeException {
public DogsNotFoundException(String message) {
super(message);
}
}