Spring Exception处理程序返回错误代码列表

时间:2019-03-20 07:51:55

标签: java spring spring-boot

如何在春季Option Explicit Sub test1() Dim LastRow As Long, i As Long, y As Long, Count As Long Dim Average As Double, Total As Double Dim CurrentTime As Date, Plus5Minutes As Date With ThisWorkbook.Worksheets("Sheet1") LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row For i = 1 To LastRow CurrentTime = .Range("A" & i).Value Plus5Minutes = DateAdd("n", 5, CurrentTime) Count = 1 Total = .Range("B" & i).Value For y = i + 2 To LastRow If .Range("A" & y).Value < Plus5Minutes Then Count = Count + 1 Total = Total + .Range("B" & y).Value Else .Range("C" & y - 1).Value = Total / Count End If Next y Next i End With End Sub 类中返回List<DataStruture>错误代码?我尝试了以下代码:

ExceptionHandler

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

2 个答案:

答案 0 :(得分:0)

您应将错误列表绑定在ResponseEntity中,然后将其返回。

 @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);
    }

答案 1 :(得分:0)

例如,如果异常为ConstrainViolationException:此异常报告约束违反的结果

@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());
}