先贴出整个SpringBoot项目的结构
1.首先新建一个web项目
2.修改pom.xml配置文件,引入SpringBoot的核心依赖
<dependencies>
<!--SpringBoot核心模块,包括自动配置支持、日志和YAML-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--测试模块,包括JUnit、Hamcrest、Mockito-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Web模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3.新建一个启动类,SpringBoot项目都有一个启动类,运行SpringBoot项目时,直接运行启动类即可(启动类好像只能放在java目录下,最外层包中,如我的启动类放在com.example.demo包中)
/**
* SpringBoot项目启动类
*/
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
//启动时关闭Banner
// SpringApplicationBuilder builder = new SpringApplicationBuilder(DemoApplication.class);
// builder.bannerMode(Banner.Mode.OFF).run(args);
}
}
4.在resources目录下新建一个application.yml或application.properties配置文件(.yml和.properties都可以,看个人喜好,了解yml语法之后个人偏向于使用yml)
#服务配置信息
server:
address: localhost
context-path: /lilu
port: 8080
访问地址:http://localhost:8080/lilu
基本搭建已经完成,接下来可以写自己的请求接口
@RestController
public class HelloController {
@Value("${person.name}")
private String name;
@RequestMapping("/hello")
public String hello(){
System.out.println(name);
return "hello"+name;
}
}
这里@Value中的${person.name}是我在yml配置文件中配置过的
#服务配置信息
server:
address: localhost
context-path: /lilu
port: 8080
tomcat:
uri-encoding: utf-8
person:
name: 藜芦
完成之后,运行 启动类,访问:http://localhost:8080/lilu/hello 即可