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