spring boot 快速入门

通过构建简单的REST应用,了解spring boot的开发基本流程,验证其简单、易用特性。

环境要求

Spring Boot 2.0.0.BUILD-SNAPSHOT 要求 Java 8 和 Spring Framework 5.0.2以上,Maven 3.2 以上或者Gradle 4。

本文使用 Spring Boot 1.5.9 、 Java8 和 Spring Framework 5.0.2.RELEASE以上,Maven 3.2。开发工具使用sping官方提供的spring suit tool 3.9.1(STS)。



创建项目

在STS中,通过NEW->Spring starter project创建spring boot 项目。

 【720开发】 spring boot 快速入门

 

输入maven的group 和artifact。

 【720开发】 spring boot 快速入门

 

选择spring boot版本和starter

 【720开发】 spring boot 快速入门

 点击下一步,进入如下界面。

选择spring boot的版本,这里选择1.5.9 版本。

选择starter,通过搜索找到web 并勾选。点击完成。

 【720开发】 spring boot 快速入门

 

创建项目的结构

点击finish 按钮。创建项目如下:

 【720开发】 spring boot 快速入门

目录结构如图。

  • Src/main/java。编写代码存放的目录。自动生成了程序入口代码 SpringBootDemo1Application.java。
  • Src/main/resources。资源文件存放目录。自动生成了配置文件 application.properties
  • Src/test/java。测试代码存放目录。自动生成了测试代码SpringBootDemo1ApplicationTests.java

POM文件说明

 spring boot项目默认使用maven来构建,生成的POM文件如下:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.yuny</groupId>
    <artifactId>demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <name>spring-boot-demo1</name>
    <description>Demo project for Spring Boot</description>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/>
    </parent>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

  

其中,

设置spring-boot-starter-parent为父亲项目

这种方式可以比较容易的使用父项目中的starters的依赖。 当然也可以不用继承spring-boot-starter-parent为父亲,这种方式在以后我们会介绍。

1
2
3
4
5
6
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/>
</parent>

 

引入web依赖

Web starter依赖引入,会增加web容器、springweb、springmvc、jackson-databind等相关的依赖。

1
2
3
4
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

依赖层级关系如图

 【720开发】 spring boot 快速入门

 

 

引入测试依赖

1
2
3
4
5
<dependency>
    <groupId>org.springframework.boot</groupId>    
        <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

 

启动程序SpringBootDemo1Application 说明  

我们通过此类的main函数来启动spring boot程序。

启动程序SpringBootDemo1Application是自动生成的,代码如下:

【720开发】 spring boot 快速入门
@SpringBootApplication
public class SpringBootDemo1Application {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemo1Application.class, args);
    }
}
【720开发】 spring boot 快速入门

 其中是@SpringBootApplication组合注解,兼备了@EnableAutoConfiguration和@ComponentScan 注解的功能。

 

增加一个controller

在包com.yuny.demo1.controller下面增加类SampleController

1
2
3
4
5
6
7
@RestController
public class SampleController {
    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }
}

运行启动程序后,访问http://localhost:8080/就可以访问这个controller的功能了。

启动很简单,直接选择SpringBootDemo1Application.java文件,使用java application方式运行即可:

 【720开发】 spring boot 快速入门

 

访问效果:

【720开发】 spring boot 快速入门

测试

增加一个测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
mport static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MockServletContext.class)
@WebAppConfiguration
public class SampleControllerTest {
    private MockMvc mock;
     
    @Before
    public void setUp() throws Exception {
        mock = MockMvcBuilders.standaloneSetup(new SampleController()).build();
    }
 
    @Test
    public void testHome() throws Exception {
        mock.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().string(equalTo("Hello World!")));
    }
}

 

本文案例代码地址

https://github.com/junyanghuang/spring-boot-samples/tree/master/springb-01-first

相关文章: