spring - RESTful 服务的 Spring Boot 异常(错误)处理
问题描述
我有以下内容RESTful Services method
:
@PostMapping("/ajouterNewField")
public String ajouterField(@Valid @ModelAttribute("field") Fields field, Model model) throws IOException {
fieldDao.save(field);
// SOME CODE
return displayListeChamps( model);
}
该方法运行良好,我的问题是如何处理任何错误(数据库未连接......)或在执行 this 期间可能发生的每个问题RESTful Services method
。
解决方案
基本上,对于您的方法可能抛出的任何类型的异常,您都必须使用一种异常处理程序:
public class FooController{
// ...
@ExceptionHandler({ CustomException1.class, CustomException2.class })
public void handleException() {
//
}
}
这是一篇很好的文章:https ://www.baeldung.com/exception-handling-for-rest-with-spring
推荐阅读
- java - 如何在 Springboot 应用程序中与同一应用程序中的 Springboot 服务在同一端口上运行 Angular 5 客户端
- python - Selenium:获取页面源时发出警报
- c# - 将日期时间传递给 sql server 存储过程添加 1 秒
- reactjs - 如何使用 webpack 将拆分的块文件注入 aspx 文件?
- jenkins - 发送在 jenkins ui 生成测试结果的邮件
- sql - 如何使用 4tables 中的 case 语句连接 postgres sql 中的行
- junit - 免费标记模板 - FTL 给出新行的测试错误
- php - 使用 codeigniter 进行表单验证时出错
- azure - 是否需要有 azure 备份计划?
- javascript - 当在字段中按下 Enter 时,以编程方式模拟对元素的单击