release- 添加校验报错

release-2024-08-08
pikaqiudeshujia 7 months ago
parent 0ede58300b
commit 1ebbf5431c

@ -1,24 +1,51 @@
package controller; package controller;
import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import api.Result; import api.Result;
import api.StatusCode; import api.StatusCode;
import dto.UserLoginDto; import dto.UserLoginDto;
import exception.BizException; import exception.BizException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;
import java.util.StringJoiner;
import java.util.stream.Collectors;
@Slf4j @Slf4j
public abstract class AbstractBaseExceptionHandler extends BaseController { public abstract class AbstractBaseExceptionHandler extends BaseController {
/**
*
*
* @param e
*/
@ExceptionHandler(value = MethodArgumentNotValidException.class)
@ResponseBody
public Result<?> handle(MethodArgumentNotValidException e) {
BindingResult bindingResult = e.getBindingResult();
StringJoiner joiner = new StringJoiner(";");
for (ObjectError error : bindingResult.getAllErrors()) {
String code = error.getCode();
String[] codes = error.getCodes();
String property = codes[1];
property = property.replace(code, "").replaceFirst(".", "");
String defaultMessage = error.getDefaultMessage();
joiner.add(property + defaultMessage);
}
return Result.failed(StatusCode.MUSIC_COMMON_FAILED, joiner.toString());
}
@ExceptionHandler(value = Exception.class) @ExceptionHandler(value = Exception.class)
@ResponseBody @ResponseBody
public Result<String> error(Exception e, HttpServletRequest request) { public Result<String> error(Exception e, HttpServletRequest request) {

Loading…
Cancel
Save