import com.example.demo.pojo.ErrorMsg;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
/**
* @Author
* @Date:
*/
@ControllerAdvice
public class ExceptionHandlerContrller {
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public List<ErrorMsg> exception(MethodArgumentNotValidException e) {
BindingResult bindingResult = e.getBindingResult();
List<ObjectError> allErrors = bindingResult.getAllErrors();
List<ErrorMsg> errorMsgs = new ArrayList<>();
allErrors.forEach(objectError -> {
ErrorMsg errorMsg = new ErrorMsg();
FieldError fieldError = (FieldError)objectError;
errorMsg.setField(fieldError.getField());
errorMsg.setObjectName(fieldError.getObjectName());
errorMsg.setMessage(fieldError.getDefaultMessage());
errorMsgs.add(errorMsg);
});
return errorMsgs;
}
}
网友评论