com.paas.service.impl.MethodArgumentNotValidExceptionResolveImpl Maven / Gradle / Ivy
package com.paas.service.impl;
import com.alibaba.fastjson.JSON;
import com.paas.bean.vo.ResponseVO;
import com.paas.service.HandlerResolveException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @ClassName MethodArgumentNotValidExceptionResolveImpl
* @Date 2021/2/2 13:44
* @Auther wangyongyong
* @Version 1.0
* @Description TODO
*/
public class MethodArgumentNotValidExceptionResolveImpl implements HandlerResolveException
{
@Override
public Class supportException(Object handler)
{
return MethodArgumentNotValidException.class;
}
@Override
public Map doResolveException(MethodArgumentNotValidException ex)
{
BindingResult bindingResult = ex.getBindingResult();
if (bindingResult.hasErrors())
{
ResponseVO> vo = new ResponseVO<>();
List messages = new ArrayList<>();
vo.setStatus(HttpStatus.BAD_REQUEST.value());
vo.setMessage(HttpStatus.BAD_REQUEST.getReasonPhrase());
List fieldErrors = bindingResult.getFieldErrors();
fieldErrors.forEach(fieldError ->
{
messages.add(fieldError.getDefaultMessage());
});
vo.setData(messages);
Map map = JSON.parseObject(JSON.toJSONString(vo), Map.class);
return map;
}
return null;
}
@Override
public int getOrder()
{
return LOWEST_PRECEDENCE;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy