Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring引导捕获多个异常并作为错误响应发送_Spring_Spring Boot_Error Handling_Json Api_Controller Advice - Fatal编程技术网

Spring引导捕获多个异常并作为错误响应发送

Spring引导捕获多个异常并作为错误响应发送,spring,spring-boot,error-handling,json-api,controller-advice,Spring,Spring Boot,Error Handling,Json Api,Controller Advice,我正在验证传入的POST请求,该请求将在验证请求数据后创建一个数据库实体。我试图在一个请求中收集多个错误,并按照JSON API规范作为错误响应进行响应: 是否可以通过@ControllerAdvice执行此操作。当全局异常处理由@ControllerAdvice启用并引发异常时,下一个异常将不会被捕获。不直接捕获。不确定您的业务案例/逻辑是什么,因此我不知道您如何在服务层处理这些异常,但一般来说,如果要在@ExceptionHandler中传递多个错误,可以创建自定义POJO: public

我正在验证传入的POST请求,该请求将在验证请求数据后创建一个数据库实体。我试图在一个请求中收集多个错误,并按照JSON API规范作为错误响应进行响应:


是否可以通过
@ControllerAdvice
执行此操作。当全局异常处理由
@ControllerAdvice
启用并引发异常时,下一个异常将不会被捕获。

不直接捕获。不确定您的业务案例/逻辑是什么,因此我不知道您如何在服务层处理这些异常,但一般来说,如果要在@ExceptionHandler中传递多个错误,可以创建自定义POJO:

public class MyError {
    private String status;
    private String source;
    private String title;
    private String detail;

    getters/setters...
}
然后创建一个自定义RuntimeException,它将接受以下POJO的列表:

public class MyRuntimeException extends RuntimeException {
    private final List<MyError> errors;

    public MyRuntimeException(List<MyError> errors) {
        super();
        this.errors = errors;
    }

    public List<MyError> getErrors() {
        return errors;
    }
}
公共类MyRuntimeException扩展RuntimeException{
私人最终名单错误;
公共MyRuntimeException(列表错误){
超级();
这个。错误=错误;
}
公共列表getErrors(){
返回错误;
}
}
在服务层中,您可以创建这些POJO的列表,然后将其包装到异常中并抛出它。然后,在@ControllerAdvice中,您只需捕获异常并调用accessor方法,对POJO列表进行迭代,以构造所需的有效负载。 比如:

@ExceptionHandler (MyRuntimeException.class)
@ResponseStatus (BAD_REQUEST)
@ResponseBody
public Map<String, Object> handleMyRuntimeException(MyRuntimeException e) {
    return singletonMap("errors", e.getErrors());
}
@ExceptionHandler(MyRuntimeException.class)
@响应状态(错误请求)
@应答器
公共映射句柄MyRuntimeException(MyRuntimeException e){
返回singletonMap(“errors”,例如getErrors());
}

不直接,不。不确定您的业务案例/逻辑是什么,因此我不知道您如何在服务层处理这些异常,但一般来说,如果您想在@ExceptionHandler中传递多个错误,您可以创建一个自定义POJO:

public class MyError {
    private String status;
    private String source;
    private String title;
    private String detail;

    getters/setters...
}
然后创建一个自定义RuntimeException,它将接受以下POJO的列表:

public class MyRuntimeException extends RuntimeException {
    private final List<MyError> errors;

    public MyRuntimeException(List<MyError> errors) {
        super();
        this.errors = errors;
    }

    public List<MyError> getErrors() {
        return errors;
    }
}
公共类MyRuntimeException扩展RuntimeException{
私人最终名单错误;
公共MyRuntimeException(列表错误){
超级();
这个。错误=错误;
}
公共列表getErrors(){
返回错误;
}
}
在服务层中,您可以创建这些POJO的列表,然后将其包装到异常中并抛出它。然后,在@ControllerAdvice中,您只需捕获异常并调用accessor方法,对POJO列表进行迭代,以构造所需的有效负载。 比如:

@ExceptionHandler (MyRuntimeException.class)
@ResponseStatus (BAD_REQUEST)
@ResponseBody
public Map<String, Object> handleMyRuntimeException(MyRuntimeException e) {
    return singletonMap("errors", e.getErrors());
}
@ExceptionHandler(MyRuntimeException.class)
@响应状态(错误请求)
@应答器
公共映射句柄MyRuntimeException(MyRuntimeException e){
返回singletonMap(“errors”,例如getErrors());
}