首页 > 解决方案 > 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

标签: springrestspring-boot

解决方案


基本上,对于您的方法可能抛出的任何类型的异常,您都必须使用一种异常处理程序:

public class FooController{

    //  ...
    @ExceptionHandler({ CustomException1.class, CustomException2.class })
    public void handleException() {
    //
    }
}

这是一篇很好的文章:https ://www.baeldung.com/exception-handling-for-rest-with-spring


推荐阅读