首页 > 技术文章 > Spring Boot 设置启动时路径和端口号

jiangjian123 2019-09-03 15:04 原文

端口号设置

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

 

推荐阅读