Spring Boot 入门

本贴最后更新于 2744 天前,其中的信息可能已经东海扬尘

简介

在您第1次接触和学习Spring框架的时候,是否因为其繁杂的配置而退却了?在你第n次使用Spring框架的时候,是否觉得一堆反复黏贴的配置有一些厌烦?那么您就不妨来试试使用Spring Boot来让你更易上手,更简单快捷地构建Spring应用!

Spring Boot让我们的Spring应用变的更轻量化。比如:你可以仅仅依靠一个Java类来运行一个Spring引用。你也可以打包你的应用为jar并通过使用java -jar来运行你的Spring Web应用。

Spring Boot的主要优点:

  • 为所有Spring开发者更快的入门
  • 开箱即用,提供各种默认配置来简化项目配置
  • 内嵌式容器简化Web项目
  • 没有冗余代码生成和XML配置的要求

快速入门

本章主要目标完成Spring Boot基础项目的构建,并且实现一个简单的Http请求处理,通过这个例子对Spring Boot有一个初步的了解,并体验其结构简单、开发快速的特性。

系统要求:

  • Java 7及以上
  • Spring Framework 4.1.5及以上

本文采用Java 1.8.0_73、Spring Boot 1.3.2调试通过。

使用Maven构建项目

  1. 通过SPRING INITIALIZR工具产生基础项目
    1. 访问:http://start.spring.io/
    2. 选择构建工具Maven Project、Spring Boot版本1.3.2以及一些工程基本信息,可参考下图所示SPRING INITIALIZRSPRING INITIALIZR
    3. 点击Generate Project下载项目压缩包
  2. 解压项目包,并用IDE以Maven项目导入,以IntelliJ IDEA 14为例:
    1. 菜单中选择File–>New–>Project from Existing Sources...
    2. 选择解压后的项目文件夹,点击OK
    3. 点击Import project from external model并选择Maven,点击Next到底为止。
    4. 若你的环境有多个版本的JDK,注意到选择Java SDK的时候请选择Java 7以上的版本

项目结构解析

项目结构项目结构

通过上面步骤完成了基础项目的创建,如上图所示,Spring Boot的基础结构共三个文件(具体路径根据用户生成项目时填写的Group所有差异):

  • src/main/java下的程序入口:Chapter1Application
  • src/main/resources下的配置文件:application.properties
  • src/test/下的测试入口:Chapter1ApplicationTests

生成的Chapter1Application和Chapter1ApplicationTests类都可以直接运行来启动当前创建的项目,由于目前该项目未配合任何数据访问或Web模块,程序会在加载完Spring之后结束运行。

引入Web模块

当前的pom.xml内容如下,仅引入了两个模块:

  • spring-boot-starter:核心模块,包括自动配置支持、日志和YAML
  • spring-boot-starter-test:测试模块,包括JUnit、Hamcrest、Mockito
				
1
			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				2
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				3
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				4
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				5
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				6
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				7
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				8
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				9
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				10
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				11
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				12
			</div>
				
<dependencies>
			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">dependency</span>&gt;</span>
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">groupId</span>&gt;</span>org.springframework.boot<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;/<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">groupId</span>&gt;</span>
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">artifactId</span>&gt;</span>spring-boot-starter<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;/<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">artifactId</span>&gt;</span>
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;/<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">dependency</span>&gt;</span>
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">dependency</span>&gt;</span>
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">groupId</span>&gt;</span>org.springframework.boot<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;/<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">groupId</span>&gt;</span>
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">artifactId</span>&gt;</span>spring-boot-starter-test<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;/<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">artifactId</span>&gt;</span>
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">scope</span>&gt;</span>test<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;/<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">scope</span>&gt;</span>
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;/<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">dependency</span>&gt;</span>
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;/<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">dependencies</span>&gt;</span>
			</div>

引入Web模块,需添加spring-boot-starter-web模块:

				
1
			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				2
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				3
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				4
			</div>
				
<dependency>
			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">groupId</span>&gt;</span>org.springframework.boot<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;/<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">groupId</span>&gt;</span>
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">artifactId</span>&gt;</span>spring-boot-starter-web<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;/<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">artifactId</span>&gt;</span>
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">&lt;/<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">dependency</span>&gt;</span>
			</div>

编写HelloWorld服务

  • 创建package命名为com.didispace.web(根据实际情况修改)
  • 创建HelloController类,内容如下
				
1
			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				2
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				3
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				4
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				5
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				6
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				7
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				8
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				9
			</div>
				
@RestController
			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">public</span> <span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;"><span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">class</span> <span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#FFFFFF;">HelloController</span> </span>{
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#75715E;">@RequestMapping</span>(<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#A6E22E;">"/hello"</span>)
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;"><span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">public</span> String <span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#A6E22E;">index</span><span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">()</span> </span>{
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">return</span> <span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#A6E22E;">"Hello World"</span>;
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				}
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				}
			</div>
  • 启动主程序,打开浏览器访问http://localhost:8080/hello,可以看到页面输出Hello World

编写单元测试用例

打开的src/test/下的测试入口Chapter1ApplicationTests类。下面编写一个简单的单元测试来模拟http请求,具体如下:

				
1
			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				2
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				3
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				4
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				5
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				6
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				7
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				8
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				9
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				10
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				11
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				12
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				13
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				14
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				15
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				16
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				17
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				18
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				19
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				20
			</div>
				
@RunWith(SpringJUnit4ClassRunner.class)
			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#75715E;">@SpringApplicationConfiguration</span>(classes = MockServletContext.class)
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#75715E;">@WebAppConfiguration</span>
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">public</span> <span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;"><span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">class</span> <span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#FFFFFF;">Chapter1ApplicationTests</span> </span>{
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">private</span> MockMvc mvc;
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#75715E;">@Before</span>
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;"><span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">public</span> <span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">void</span> <span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#A6E22E;">setUp</span><span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">()</span> <span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">throws</span> Exception </span>{
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				mvc = MockMvcBuilders.standaloneSetup(<span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">new</span> HelloController()).build();
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				}
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#75715E;">@Test</span>
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;"><span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">public</span> <span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">void</span> <span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#A6E22E;">getHello</span><span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">()</span> <span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">throws</span> Exception </span>{
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				mvc.perform(MockMvcRequestBuilders.get(<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#A6E22E;">"/hello"</span>).accept(MediaType.APPLICATION_JSON))
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				.andExpect(status().isOk())
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				.andExpect(content().string(equalTo(<span style="font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#A6E22E;">"Hello World"</span>)));
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				}
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				}
			</div>

使用MockServletContext来构建一个空的WebApplicationContext,这样我们创建的HelloController就可以在@Before函数中创建并传递到MockMvcBuilders.standaloneSetup()函数中。

  • 注意引入下面内容,让status、content、equalTo函数可用
				
1
			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				2
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				3
			</div>
				
import static org.hamcrest.Matchers.equalTo;
			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">import</span> <span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">static</span> org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
			</div>

			<div style="margin:0px;padding:0px;border:0px;font-weight:inherit;font-style:inherit;font-family:inherit;vertical-align:baseline;">
				<span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">import</span> <span style="font-weight:bold;font-style:inherit;font-family:inherit;vertical-align:baseline;color:#F92672;">static</span> org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
			</div>

至此已完成目标,通过Maven构建了一个空白Spring Boot项目,再通过引入web模块实现了一个简单的请求处理。

  • Spring

    Spring 是一个开源框架,是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作《Expert One-On-One J2EE Development and Design》中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 JavaEE 应用程序开发提供集成的框架。

    944 引用 • 1459 回帖 • 26 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...