Spring Boot 設置啟動時路徑和端口號


端口號設置

  • 配置文件中設置(application.yml)
server:
  port: 8888
  • 配置文件中設置(application.properties)
server.port: 8888
也可以在代碼中硬編碼設置端口號(不推薦)

設置路徑

  • springboot 2.x以上版本(server.servlet.context-path)
  1. 配置文件中設置(application.yml)
server:
  servlet:
    context-path: /news
  1. 配置文件中設置(application.properties)
server.servlet.context-path: /news
  • Spring Boot老版本的配置路徑
  1. 配置文件中設置(application.yml)
server:
  context-path: /news
  1. 配置文件中設置(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進行轉發,十分方便。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM