SpringBoot配置項目訪問路徑URL的根路徑方式
配置項目訪問路徑URL的根路徑
1.SpringBoot在2.0之前版本
使用server.context-path
server.context-path=/api
2.SpringBoot在2.0之后版本
使用server.servlet.context-path
server.servlet.context-path=/api
設(shè)置默認訪問路徑
一共有兩種方法。
1.繼承WebMvcConfigurerAdapter類或?qū)崿F(xiàn)WebMvcConfigurer接口
創(chuàng)建一個config包,然后在包內(nèi)創(chuàng)建MyMvcConfig類。
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.WebMvcConfigurerAdapter; ? @Configuration public class MyMvcConfig extends WebMvcConfigurerAdapter { ? ? @Override ? ? public void addViewControllers(ViewControllerRegistry registry) {? ? ? ? ? registry.addViewController("/").setViewName("index");? ? ? ? ? registry.setOrder(Ordered.HIGHEST_PRECEDENCE);? ? ? ? ? super.addViewControllers(registry);? ? ? }? }
注意:如果用這個方法html頁面需要在static下,不然會出現(xiàn)404錯誤,找不到頁面。
2.@Controller路由設(shè)置
在controller層中創(chuàng)建一個IndexController類
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;? @Controller public class IndexController { ? ? @RequestMapping({"/","/index"}) ? ? public String index(){ ? ? ? ? return "index"; ? ? } }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 類型相互轉(zhuǎn)換byte[]類型,Blob類型詳細介紹
這篇文章主要介紹了Java 類型相互轉(zhuǎn)換byte[]類型,Blob類型的相關(guān)資料,需要的朋友可以參考下2016-10-10java byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10MapStruct處理Java中實體與模型間不匹配屬性轉(zhuǎn)換的方法
今天小編就為大家分享一篇關(guān)于MapStruct處理Java中實體與模型間不匹配屬性轉(zhuǎn)換的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03