首页 > 编程语言 > 详细

Spring Boot 设置启动时路径和端口号

时间:2019-09-03 15:22:04      阅读:131      评论:0      收藏:0      [点我收藏+]

端口号设置

  • 配置文件中设置(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进行转发,十分方便。

 

Spring Boot 设置启动时路径和端口号

原文:https://www.cnblogs.com/jiangjian123/p/11424359.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!