zuul(springboot)設置靜態資源代理和默認首頁代碼一例


直接上代碼:

public class StaticResourcesConfig extends WebMvcConfigurerAdapter {
private static final Log log = LogFactory.getLog(StaticResourcesConfig.class);

    @Override
    /**
     * 設置默認首頁
     */
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("redirect:/index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/path/**").addResourceLocations("file:/html/);

    }
}

第二個方法可以添加靜態資源映射,

在linux下,如果通過http://[server ip]/path/index.html進行訪問,則可以訪問到磁盤路徑為  /html/index.html 的文件

在windows下則可以將代碼中的 /html/ 換成 [盤符]:/html/ D:/html/

 

提醒一下:

如果用的是容器,不要忘記目錄映射


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM