1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package com.jhj.gulimall.product.exception;
import com.jhj.common.exception.BizCodeEnume; import com.jhj.common.utils.R; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.HashMap; import java.util.Map;
@Slf4j
@RestControllerAdvice(basePackages = "com.jhj.gulimall.product.controller") public class GulimallExceptionControllerAdvice {
@ExceptionHandler(value = MethodArgumentNotValidException.class) public R handleVaildExceptinon(MethodArgumentNotValidException e){ log.error("检验错误{}",e.getMessage()); HashMap<String, String> errorMap = new HashMap<>(); e.getBindingResult().getFieldErrors().forEach((res)->{ errorMap.put(res.getField(),res.getDefaultMessage()); }); return R.error(BizCodeEnume.VAILD_EXCEPTION.getCode(), BizCodeEnume.VAILD_EXCEPTION.getMsg()).put("data",errorMap); }
@ExceptionHandler(value = Throwable.class) public R handleVaildExceptinon(Throwable e){
return R.error(BizCodeEnume.UNKNOW_EXCEPTION.getCode(), BizCodeEnume.UNKNOW_EXCEPTION.getMsg()); } }
|