内容简介:这篇教程主要专注于如何优雅的处理WEB中的异常。虽然我们可以手动的设置下面的代码中,这种处理异常的方式最大的问题就在于代码的重复。catch部分的代码在很多其它地方也会使用到(比如删除,更新等操作)
前言
这篇教程主要专注于如何优雅的处理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部分的代码在很多其它地方也会使用到(比如删除,更新等操作)
Controller Advice
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);
}
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 猫头鹰的深夜翻译:对于RestAPI简单的基于身份的权限控制
- 基于 Laravel、Lumen 框架集成百度翻译、有道翻译、Google 翻译扩展包
- 腾讯发布人工智能辅助翻译 致敬人工翻译
- golang调用baidu翻译api实现自动翻译
- 监管机器翻译质量?且看阿里如何搭建翻译质量评估模型
- 机器翻译新突破:谷歌实现完全基于attention的翻译架构
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Twisted Network Programming Essentials
Abe Fettig / O'Reilly Media, Inc. / 2005-10-20 / USD 29.95
Developing With Python's Event-driven Framework一起来看看 《Twisted Network Programming Essentials》 这本书的介绍吧!