端口號設置
- 配置文件中設置(application.yml)
server:
port: 8888
- 配置文件中設置(application.properties)
server.port: 8888
也可以在代碼中硬編碼設置端口號(不推薦)
設置路徑
- springboot 2.x以上版本(server.servlet.context-path)
- 配置文件中設置(application.yml)
server:
servlet:
context-path: /news
- 配置文件中設置(application.properties)
server.servlet.context-path: /news
- Spring Boot老版本的配置路徑
- 配置文件中設置(application.yml)
server:
context-path: /news
- 配置文件中設置(application.properties)
server.context-path: /news
假如在Controller配置如下:
@RequestMapping("/v1")
public class NewsController {
@Autowired
NewsService newsService;
@GetMapping("/queryByName")
public String queryByName(@RequestParam(value = "name" ,required = true) String name,
@RequestParam(value = "num",required = false)Integer num) {
return name+" :"+ num;
}
則訪問路徑為:http://IP+PROT/news/v1/queryByName
- 建議配置了context-path
配置了context-path后,nginx中可以根據context-path進行轉發,十分方便。