参考

https://blog.csdn.net/HD243608836/article/details/116239908

 

package net.hp.exception;

import net.hp.common.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常处理器
 *
 * @author gmh
 */
@RestControllerAdvice
public class RRExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 处理自定义异常
     */

  @ExceptionHandler(MethodArgumentNotValidException.class)
  public R handleBindingException(MethodArgumentNotValidException e){
    logger.error(e.getMessage(), e);
    return R.error(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
  } 

}

 

posted on 2022-07-19 16:42  1161588342  阅读(74)  评论(0编辑  收藏  举报