|
| 1 | +package com.web.spring.databind; |
| 2 | + |
| 3 | +import com.web.spring.databind.domain.ResponseError; |
| 4 | +import com.web.spring.databind.domain.SimpleUser; |
| 5 | +import java.util.List; |
| 6 | +import javax.validation.Valid; |
| 7 | +import org.springframework.http.HttpStatus; |
| 8 | +import org.springframework.http.ResponseEntity; |
| 9 | +import org.springframework.stereotype.Controller; |
| 10 | +import org.springframework.validation.BindingResult; |
| 11 | +import org.springframework.validation.FieldError; |
| 12 | +import org.springframework.validation.ObjectError; |
| 13 | +import org.springframework.web.bind.MethodArgumentNotValidException; |
| 14 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 15 | +import org.springframework.web.bind.annotation.GetMapping; |
| 16 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 17 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 18 | +import org.springframework.web.bind.annotation.ResponseStatus; |
| 19 | + |
| 20 | +/** |
| 21 | + * Created by Administrator on 2017/7/9. |
| 22 | + */ |
| 23 | +@Controller |
| 24 | +@RequestMapping(value = "/validate") |
| 25 | +public class ValidateData { |
| 26 | + /** |
| 27 | + * TODO http://localhost:18080/validate/user?name=sun&age=27 |
| 28 | + * 使用注解校验返回客户端错误信息 |
| 29 | + */ |
| 30 | + @GetMapping(value = "/user", produces = "application/json") |
| 31 | + public ResponseEntity getSimpleObject(@Valid SimpleUser user, BindingResult result) { |
| 32 | + if (result.hasErrors()) { |
| 33 | + return new ResponseEntity(getFieldErrors(result), HttpStatus.BAD_REQUEST); |
| 34 | + } |
| 35 | + return new ResponseEntity(user, HttpStatus.OK); |
| 36 | + } |
| 37 | + |
| 38 | +// @ExceptionHandler( value = {MethodArgumentNotValidException.class}) |
| 39 | +// @ResponseStatus(HttpStatus.BAD_REQUEST) |
| 40 | +// @ResponseBody |
| 41 | +// public String validationError(MethodArgumentNotValidException ex) { |
| 42 | +// BindingResult result = ex.getBindingResult(); |
| 43 | +// final List<FieldError> fieldErrors = result.getFieldErrors(); |
| 44 | +// StringBuilder builder = new StringBuilder(256); |
| 45 | +// for (FieldError fieldError : fieldErrors) { |
| 46 | +// builder.append("value:") |
| 47 | +// .append(fieldError.getField()) |
| 48 | +// .append(" msg:") |
| 49 | +// .append(fieldError.getDefaultMessage()) |
| 50 | +// .append(" "); |
| 51 | +// } |
| 52 | +// return builder.toString(); |
| 53 | +// } |
| 54 | + |
| 55 | + private String getFieldErrors(BindingResult result) { |
| 56 | + final List<FieldError> fieldErrors = result.getFieldErrors(); |
| 57 | + StringBuilder builder = new StringBuilder(256); |
| 58 | + if (result.hasFieldErrors()) { |
| 59 | + builder.append("Field error:"); |
| 60 | + for (FieldError fieldError : fieldErrors) { |
| 61 | + builder.append("value:") |
| 62 | + .append(fieldError.getField()) |
| 63 | + .append(" msg:") |
| 64 | + .append(fieldError.getDefaultMessage()) |
| 65 | + .append(" "); |
| 66 | + } |
| 67 | + builder.append('\n'); |
| 68 | + } |
| 69 | + if (result.hasGlobalErrors()) { |
| 70 | + builder.append("Global error:"); |
| 71 | + for (ObjectError objectError : result.getGlobalErrors()) { |
| 72 | + builder.append("errorCode:") |
| 73 | + .append(objectError.getCode()) |
| 74 | + .append("msg:") |
| 75 | + .append(objectError.getDefaultMessage()) |
| 76 | + .append(" "); |
| 77 | + } |
| 78 | + } |
| 79 | + return builder.toString(); |
| 80 | + } |
| 81 | +} |
0 commit comments