@RestControllerAdvice注解会应用到所有@RequestMapping中。
直接上代码吧
package com.example.jwt2021921.core;
import com.auth0.jwt.exceptions.TokenExpiredException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.servlet.http.HttpServletRequest;
@RestControllerAdvice
@Slf4j
public class GlobalException {
// 捕捉其他所有异常
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.OK)
public RestResponse globalException(Exception e) {
log.error("服务器异常:", e);
return RestResponse.failed("服务器异常",e.getMessage());
}
@ExceptionHandler(TokenExpiredException.class)
public RestResponse tokenExc(Exception e){
log.error("to