SpringBoot 全局异常处理404
导读
SpringBoot全局异常处理404问题
修改配置文件
spring.mvc.throw-exception-if-no-handler-found=true
spring.web.resources.add-mappings=false
全局异常拦截器
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(NoHandlerFoundException.class)
public String handleNotFound(HttpServletRequest req, NoHandlerFoundException ex) {
log.error("请求路径不存在:{}", req.getRequestURI());
return "404 请求资源不存在";
}
}

浙公网安备 33010602011771号