|
4 | 4 | import org.springframework.core.annotation.Order;
|
5 | 5 | import org.springframework.http.HttpStatus;
|
6 | 6 | import org.springframework.validation.BindException;
|
7 |
| -import org.springframework.validation.FieldError; |
8 | 7 | import org.springframework.web.bind.annotation.ExceptionHandler;
|
9 | 8 | import org.springframework.web.bind.annotation.ResponseStatus;
|
10 | 9 | import org.springframework.web.bind.annotation.RestControllerAdvice;
|
@@ -35,15 +34,15 @@ public String handleConstraintViolationException(ConstraintViolationException e)
|
35 | 34 | @ExceptionHandler({BindException.class})
|
36 | 35 | @ResponseStatus(HttpStatus.BAD_REQUEST)
|
37 | 36 | public String handleBindException(BindException e) {
|
38 |
| - StringBuilder message = new StringBuilder(); |
39 |
| - FieldError fieldError = e.getFieldError(); |
40 |
| - message.append("Object:") |
41 |
| - .append(e.getObjectName()) |
42 |
| - .append(" Field:") |
43 |
| - .append(fieldError.getField()) |
44 |
| - .append(" Message:") |
45 |
| - .append(fieldError.getDefaultMessage()); |
46 |
| - return message.toString(); |
| 37 | + StringBuilder stringBuilder = new StringBuilder(); |
| 38 | + e.getFieldErrors().forEach(item ->{ |
| 39 | + // 获取错误的属性的名字 |
| 40 | + String field = item.getField(); |
| 41 | + // 获取到错误提示 |
| 42 | + String message = item.getDefaultMessage(); |
| 43 | + stringBuilder.append(field).append("-").append(message).append(", "); |
| 44 | + }); |
| 45 | + return stringBuilder.toString(); |
47 | 46 | }
|
48 | 47 |
|
49 | 48 | }
|
0 commit comments