創建SpringBoot項目,啟動后,默認的訪問路徑即主機IP+默認端口號8080:http://localhost:8080/
此時,我們就可以訪問Controller層的接口了,如:http://localhost:8080/hello
package com.springboot.test;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SpringBootTest {
@RequestMapping("/hello")
public String helloSpringBoot() {
return "Hello SpringBoot Project.";
}
}
當然,我們可以通過配置來更改默認端口和項目訪問路徑:
修改端口號
使用properties文件方式:
在src/main/resoutces目錄下創建:application.properties,添加如下配置即可修改端口號:
server.port=8088`
使用yml文件方式:
在src/main/resoutces目錄下創建:application.yml,添加如下配置即可修改端口號:
server:`
port:8088`
修改項目訪問路徑
使用properties文件方式:
在application.properties,添加如下配置即可修改項目訪問路徑:
server.context-path=/springboot-demo`
使用yml文件方式:
在application.yml,追加如下配置即可修改項目訪問路徑:
server:`
port:8088`
context-path:/springboot-demo`
此時,演示properties方式的效果,如下圖:
server.port=8088`
server.context-path=/springboot-demo
以上就是本文的全部內容,希望對大家的學習有所幫助
