Springboot設(shè)置默認(rèn)訪問路徑方法實現(xiàn)
當(dāng)使用springboot與其他框架結(jié)合編寫web前后端時,可能存在這樣的需求:我想在訪問10.10.10.100時,實際上需要訪問10.10.10.100/hello頁面。(端口已省略,自行設(shè)置)
解決方案1 - 實現(xiàn)WebMvcConfigurer接口搜過很多博客,里面的內(nèi)容雖然可以用。但是基本上都是基于繼承WebMvcConfigurerAdapter類實現(xiàn)的,而官方的源碼里面已經(jīng)不推薦使用該類了。
下面給出我的解決方案,很簡單:
import org.springframework.context.annotation.Configuration;import org.springframework.core.Ordered;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configurationpublic class DefaultView implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController('/').setViewName('hello'); registry.setOrder(Ordered.HIGHEST_PRECEDENCE); }}
補(bǔ)充&注意點
setViewName :將 / 指向 /hello
這里需要注意,因為我的項目里把url的訪問路徑后綴'.html'全部都去掉了,所以可以這么用。如果你的不是,需要做對應(yīng)調(diào)整。
補(bǔ)充我的application.properties文件部分配置:
server.port=80 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.mvc.favicon.enabled=true
@Configuration 別忘了加,我自己就是忘了加,以為沒生效,折騰半天。
方案2 - @Controller路由設(shè)置@Controllerpublic class PageController { @GetMapping(value = '/') public String defaultPath(Model model) { return 'hello'; } }
properties文件配置同方案1一致
到此這篇關(guān)于Springboot設(shè)置默認(rèn)訪問路徑方法實現(xiàn)的文章就介紹到這了,更多相關(guān)Springboot 默認(rèn)訪問路徑內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. IntelliJ IDEA導(dǎo)入jar包的方法2. Python requests庫參數(shù)提交的注意事項總結(jié)3. 詳談ajax返回數(shù)據(jù)成功 卻進(jìn)入error的方法4. python ansible自動化運維工具執(zhí)行流程5. vue-electron中修改表格內(nèi)容并修改樣式6. JavaScript中l(wèi)ayim之整合右鍵菜單的示例代碼7. python操作mysql、excel、pdf的示例8. 匹配模式 - XSL教程 - 49. 通過Python pyecharts輸出保存圖片代碼實例10. javascript實現(xiàn)雪花飄落效果
