SpringBoot配置項(xiàng)目訪問路徑URL的根路徑方式
配置項(xiàng)目訪問路徑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è)置默認(rèn)訪問路徑
一共有兩種方法。
1.繼承WebMvcConfigurerAdapter類或?qū)崿F(xiàn)WebMvcConfigurer接口
創(chuàng)建一個(gè)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);?
? ? }?
}注意:如果用這個(gè)方法html頁面需要在static下,不然會(huì)出現(xiàn)404錯(cuò)誤,找不到頁面。
2.@Controller路由設(shè)置
在controller層中創(chuàng)建一個(gè)IndexController類
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;?
@Controller
public class IndexController {
? ? @RequestMapping({"/","/index"})
? ? public String index(){
? ? ? ? return "index";
? ? }
}以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring 開發(fā)過程中Value 注解的使用場(chǎng)景
這篇文章主要介紹了Spring 開發(fā)過程中Value 注解的使用場(chǎng)景,幫助大家更好的理解和使用spring框架,感興趣的朋友可以了解下2020-11-11
SpringBoot+MyBatis簡(jiǎn)單數(shù)據(jù)訪問應(yīng)用的實(shí)例代碼
這篇文章主要介紹了SpringBoot+MyBatis簡(jiǎn)單數(shù)據(jù)訪問應(yīng)用的實(shí)例代碼,需要的朋友可以參考下2017-05-05
java實(shí)現(xiàn)大文件導(dǎo)出的實(shí)現(xiàn)與優(yōu)化
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)大文件導(dǎo)出的實(shí)現(xiàn)與優(yōu)化的相關(guān)資料,文中的示例代碼講解詳細(xì),對(duì)我們深入了解java有一定的幫助,感興趣的小伙伴可以了解下2023-11-11
SpringMVC JSON數(shù)據(jù)交互實(shí)現(xiàn)過程解析
這篇文章主要介紹了SpringMVC JSON數(shù)據(jù)交互實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Java基礎(chǔ)知識(shí)之CharArrayReader流的使用
這篇文章主要介紹了Java基礎(chǔ)知識(shí)之CharArrayReader流的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

