创建SpringBoot项目,启动后,默认的访问路径即主机IP+默认端口号8080(内置Tomcat访问端口):http://localhost:8080
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
@RequestMapping("/")
public String home() {
return "hello";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
修改端口号
通过IDEA创建的springboot项目默认会在src/main/resources目录下生成application.properties,在application.properties添加如下配置即可修改端口号:
server.port=8089
如果是yml配置文件:
server:
port: 8089
运行项目,在控制台看到如下日志则表明端口已经修改成功,访问地址:http://localhost:8089/
Tomcat started on port(s): 8089 (http) with context path
修改项目访问路径
在application.properties添加如下配置即可修改访问路径:
server.servlet.context-path=/demo
如果是yml配置文件:
server:
port: 8089
servlet:
context-path: /demo
运行项目,在控制台看到如下日志则表明端口已经修改成功,访问地址:
http://localhost:8089/demo/
Tomcat started on port(s): 8089 (http) with context path '/demo'