一、建包并创建控制器
- 创建包:com.zjl.controller
- 创建控制器:HelloController
- 代码如下:
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String hello() {
System.out.println("======hello world=======");
return "hello";
}
}
4.截图如下 
二、访问项目
注意:我们没有配置项目名,所以访问路径里面没有项目名。 访问地址:http://localhost:8080/hello/hello
响应截图如下:

三、修改内嵌服务器端口
server:
port: 8989
四、修改应用名称
server:
servlet:
context-path: /Application
五、相关注解说明
入口类 SpringApplication
- SpringBootApplication: 全局入口类 有且只能有一个
- mian 函数参数可以在启动时指定jvm参数覆盖默认配置
@SpringBootApplication 注解等价于:
- @SpringBootConfiguration 标识这是一个springboot的配置类,默认自定配置Spring环境
- @EnableAutoConfiguration 自动与项目中集成的第三方技术进行集成
- @ComponentScan 扫描入口类所在子包以及子包后代包中注解
六、配置文件
配置文件的拆分:
说明: 在实际开发过程中生产环境和测试环境有可能是不一样的 因此将生产中的配置和测试中的配置拆分开,是非常必要的,在springboot中也提供了配置文件拆分的方式. 这里以生产中项名名称不一致为例:
- 生产中项目名为: springboot测
- 试中项目名为: springboot_day1
- 端口同时为: 8080
- application.yml
server:
port: 8080
- application-pord.yml
server:
servlet:
context-path: /springboot_pord
- application-dev.yml
server:
servlet:
context-path: /springboot_dev
spring:
profiles:
active: dev
配置截图如下:
1、application.yml

2、application-pord.yml

3、application-dev.yml
 启动项目,此时使用的配置文件为:application-dev.yml,所以项目名应该为:springboot_dev,端口为:8080
通过启动日志,也能看到使用的配置文件为dev文件,截图如下:

访问地址:http://localhost:8080/springboot_dev/hello/hello 结论:能正常访问到我们的controller,说明确实是使用的dev配置文件

|