Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.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
Java Spring异常处理程序返回错误代码列表_Java_Spring_Spring Boot - Fatal编程技术网

Java Spring异常处理程序返回错误代码列表

Java Spring异常处理程序返回错误代码列表,java,spring,spring-boot,Java,Spring,Spring Boot,如何在springExceptionHandler类中返回列表错误代码?我尝试了以下代码: @ExceptionHandler({ CustomException.class }) @ResponseStatus(HttpStatus.CONFLICT) public List<ErrorOutDTO> handle(CustomException theException) { return new null; } @ExceptionHandler({CustomEx

如何在spring
ExceptionHandler
类中返回
列表
错误代码?我尝试了以下代码:

@ExceptionHandler({ CustomException.class })
@ResponseStatus(HttpStatus.CONFLICT)
public List<ErrorOutDTO> handle(CustomException theException) {
    return new null;
} 
@ExceptionHandler({CustomException.class})
@ResponseStatus(HttpStatus.CONFLICT)
公共列表句柄(CustomException theException){
返回新的空值;
} 

我对方法的返回类型感到震惊。

您应该在中绑定错误列表,然后返回它

 @ExceptionHandler(CustomException.class)
    public ResponseEntity<List<ErrorOutDTO>> validationExceptionHandler(CustomException exception) {
        List<ErrorOutDTO> list = new ArrayList<>();
        ErrorOutDTO error = new ErrorOutDTO();
        error.setField(exception.getField());
        error.setMessage(exception.getMessage());
        list.add(error);
        return (ResponseEntity<List<ErrorOutDTO>>)new ResponseEntity(list, HttpStatus.CONFLICT);
    }
@ExceptionHandler(CustomException.class)
public ResponseEntity validationExceptionHandler(自定义异常){
列表=新的ArrayList();
ErrorOutDTO error=新的ErrorOutDTO();
错误.setField(异常.getField());
错误.setMessage(异常.getMessage());
列表。添加(错误);
返回(ResponseEntity)新的ResponseEntity(列表,HttpStatus.CONFLICT);
}

例如,如果异常是ConstraintViolationException:此异常报告约束冲突的结果

@ExceptionHandler({ ConstraintViolationException.class })
public ResponseEntity<Object> handleConstraintViolation(
  ConstraintViolationException ex, WebRequest request) {
    List<String> errors = new ArrayList<String>();
    for (ConstraintViolation<?> violation : ex.getConstraintViolations()) {
        errors.add(violation.getRootBeanClass().getName() + " " + 
          violation.getPropertyPath() + ": " + violation.getMessage());
    }

    ApiError apiError = 
      new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), errors);
    return new ResponseEntity<Object>(
      apiError, new HttpHeaders(), apiError.getStatus());
}
@ExceptionHandler({ConstraintViolationException.class})
公共责任处理培训暴力(
ConstraintViolationException(例如,WebRequest请求){
列表错误=新建ArrayList();
for(ConstraintViolations冲突:例如getConstraintViolations()){
错误.add(违反.getRootBeanClass().getName()+“”+
违规。getPropertyPath()+“:”+违规。getMessage());
}
APIRERROR APIRROR=
新的APIRROR(HttpStatus.BAD_请求,例如getLocalizedMessage(),错误);
返回新响应(
apierro,new-HttpHeaders(),apierro.getStatus();
}

您能否澄清一下,您希望通过此1实现什么。控制器方法发送多个HttpStatus代码2。CustomException.class
@ResponseStatus(HttpStatus.CONFLICT,HttpStatus.OK)
?在类上使用@RestControllerAdvice此选项不会覆盖我的默认响应:(