springboot mvc读取静态资源文件读取不到

    xiaoxiao2022-07-05  170

    问题:

    发现请求static/css/tree/ztree.diy.css 请求不到。

    调试过程:

    Debug 进入 springmvc DispatcherServlet -> FremeWorkServlet -> 进入doGet请求,进入springboot的默认几个资源请求组合位置:

    Public

    Resources

    static

    ...

     

    + /css/tree/ztree.diy.css

     

    结果发现 组合到static/css/tree/ztree.diy.css 获取不到resource资源。

     

    最终发现

    如下代码执行返回为空:

    ClassPathResource中 this.classLoader.getResource(this.path)

    @Nullable protected URL resolveURL() { if (this.clazz != null) { return this.clazz.getResource(this.path); } else if (this.classLoader != null) { return this.classLoader.getResource(this.path); } else { return ClassLoader.getSystemResource(this.path); } }

    返回的url为空。

    思来想去 classloader不应该获取不到文件呀。

     

    于是乎,进行maven package重新编译打包项目。再次运行就获取到了。

     

    总结:

    原来还是文件没有完全发布成功啊。。。

     

     

    补充:

    今天又遇到了所有静态资源读取不到的问题。采用同样的方法debug

    依次进入:

    org.springframework.web.servlet.DispatcherServlet#getHandler(HttpServletRequest) org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping#handleNoMatch(Set<RequestMappingInfo> infos, String lookupPath, HttpServletRequest request) org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.PartialMatchHelper#PartialMatchHelper(Set<RequestMappingInfo> infos, HttpServletRequest request)

    发现今天资源被一个post请求给拦截了

    于是查找控制器发现了罪魁祸首,使用PostMapping必须得带value,不然会拦截到静态资源的访问

     

    果然加上value后即可访问成功。

    最新回复(0)