连初级都算不上的代码小白 2023-12-27 22:34 采纳率: 52.4%
浏览 4
已结题

springboot的静态资源URI是error


package com.daming.reggie_start.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
@Slf4j
public class MvcSupport extends WebMvcConfigurationSupport {
    @Autowired
    public loginInterceptor interceptor;
    @Override
 
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("backend/**").addResourceLocations("classpath:/backend/");
    }
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("正在拦截中......");
        registry.addInterceptor(interceptor).addPathPatterns("/**");
    }
}

public class loginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String URI= request.getRequestURI();
        log.info("URI ======》"+URI);//此处URI为error
        return true;
    }
}

上面是拦截路径以及拦截器的preHandle方法、为什么URI总是error
下面是项目结构

img

controller中的URI没问题

  • 写回答

1条回答 默认 最新

  • 关注

    若继承WebMvcConfigurationSupport会继承父类的所有方法、需要重写其中的方法、
    springboot默认下是可以直接访问静态资源的、但这样的话就不能直接访问静态资源了
    最好是mplements WebMvcConfigurer 这样不会影响项目对静态资源的干扰。
    其余的再补充

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月5日
  • 已采纳回答 12月28日
  • 创建了问题 12月27日