鸿蒙原生应用开发者激励计划发布!最高获百万现金!点击立即参与
spring mvc +
@Valid
+@RequestBody 来做参数校验返回400,并且不显示具体message 如何解决
-----------------
时间2018年7月5日14:44:07
@ControllerAdvice
public class ExceptionHandlerController {
private static Logger logger = LoggerFactory.getLogger(ExceptionHandlerController.class);
@ExceptionHandler(value = {Exception.class})
public ResponseEntity hadlerException(final Exception ex, final WebRequest req){
Map<String,String> errorMap = new HashMap<>();
logger.error("拦截异常-",ex);
//参数错误
if(ex instanceof MethodArgumentNotValidException){
BindingResult result = ((MethodArgumentNotValidException) ex).getBindingResult();
wrapperError(result,errorMap);
return new ResponseEntity<Object>(errorMap, HttpStatus.OK);
//其他错误
errorMap.put("401","系统错误,请稍后再试");
return new ResponseEntity<Object>(errorMap, HttpStatus.OK);
private void wrapperError(BindingResult result,Map<String,String> errorMap){
List<ObjectError> list = result.getAllErrors();
String eMsg = "";
if(list.size() > 0){
eMsg = list.get(0).getDefaultMessage();
errorMap.put("code","102");
errorMap.put("msg",eMsg);