Validation(3)--全局参数异常校验捕获及返回XML解决
@RestControllerAdvice原创
直接上代码,后面再说怎么用
1、这个是一个Form,用来接收参数的,一个简单的NotEmpty注解校验,merchantName这个参数是必传的;
package com.cloudwalk.shark.dto.validate; import org.hibernate.validator.constraints.NotEmpty; public class AdvertQueryForm { @NotEmpty private String merchantName; private String merchantCode; private String clientCode; private String id; public String getMerchantName() { return merchantName; } public void setMerchantName(String merchantName) { this.merchantName = merchantName; } public String getMerchantCode() { return merchantCode; } public void setMerchantCode(String merchantCode) { this.merchantCode = merchantCode; } public String getClientCode() { return clientCode; } public void setClientCode(String clientCode) { this.clientCode = clientCode; } public String getId() { return id; } public void setId(String id) { this.id = id; } }
2、Controller中直接通过RequestBody取到参数,下面是重点@Validated
这个注解可以直接用来校验传递的参数;
package com.cloudwalk.shark.controller; import com.cloudwalk.shark.common.utils.ResponseData; import com.cloudwalk.shark.dto.validate.AdvertQueryForm; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(value = "/validate", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}) public class SharkValidationController { @PostMapping("/bean") @ResponseBody public ResponseData checkBeanIsValid(@Validated @RequestBody AdvertQueryForm advertQueryForm){ return new ResponseData(true,"2","3",advertQueryForm); } }
直接友好的捕获了你的参数校验异常,统一的去实现了参数异常的捕获!
重点!produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}这个参数如果不设定,默认返回的就会是XML;
package com.cloudwalk.shark.config.controllerException; import com.cloudwalk.shark.common.em.GlobalCodeEnum; import com.cloudwalk.shark.common.utils.ResponseData; import org.springframework.http.HttpStatus; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import java.util.Set; @RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(HttpStatus.OK) @ResponseBody public ResponseData handleMethodArgumentNotValidException(MethodArgumentNotValidException exception) { StringBuilder errorInfo = new StringBuilder(); BindingResult bindingResult = exception.getBindingResult(); for(int i = 0; i < bindingResult.getFieldErrors().size(); i++){ if(i > 0){ errorInfo.append(","); } FieldError fieldError = bindingResult.getFieldErrors().get(i); errorInfo.append(fieldError.getField()).append(" :").append(fieldError.getDefaultMessage()); } //返回BaseResponse ResponseData response = new ResponseData(); response.setMessage(errorInfo.toString()); response.setCode(GlobalCodeEnum.FAIL.getErrorCode()); return response; } @ExceptionHandler(ConstraintViolationException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseBody public ResponseData<String> handleConstraintViolationException(ConstraintViolationException exception) { StringBuilder errorInfo = new StringBuilder(); String errorMessage ; Set<ConstraintViolation<?>> violations = exception.getConstraintViolations(); for (ConstraintViolation<?> item : violations) { errorInfo.append(item.getMessage()).append(","); } errorMessage = errorInfo.toString().substring(0, errorInfo.toString().length()-1); ResponseData<String> response = new ResponseData<String>(); response.setMessage(errorMessage); response.setCode(GlobalCodeEnum.FAIL.getErrorCode()); return response; } @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public ResponseData<String> handleDefaultException(Exception exception) { ResponseData<String> response = new ResponseData<>(); response.setMessage("其他错误"); response.setCode(GlobalCodeEnum.FAIL.getErrorCode()); return response; } }
没有什么比每天有成长进步更高兴的事情