In this chapter, by completing the construction of the Spring Boot basic project and implementing a simple Http request processing, let everyone have a preliminary understanding of Spring Boot and experience its simple structure and rapid development characteristics. It is expected that the reading and practice process will take about 5 minutes.
Use Maven to build the project
SPRING INITIALIZR
Generate basic projects through tools- access:
http://start.spring.io/
- Fill in the group (such as com.v5ent), select the build tool
Maven Project
, Spring Boot version and some basic project information, please refer to the following figure - Click to
Generate Project
download the project compressed package
- access:
- Unzip the project package and
Maven
import it into the IDE as a project
Project structure analysis
Through the above steps, the creation of the basic project structure is completed. The basic structure of Spring Boot has three files:
src/main/java
The following program entry:DemoApplication
src/main/resources
Configuration file under:application.properties
src/test/
The following test entry:DemoApplicationTests
The generated DemoApplication
and DemoApplicationTests
classes can be run directly to start the currently created project. Since the project does not currently cooperate with any data access or web modules, the program will end after loading Spring.
Introduce web module
pom文件已经
Two modules are currently introduced:
spring-boot-starter
: Core modules, including automatic configuration support, logging and YAMLspring-boot-starter-test
: Test modules, including JUnit, Hamcrest, Mockito
We need to add spring-boot-starter-web
modules:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Write HelloWorld service
- Created
package
namedcom.v5ent.demo.web
- Create a
HelloController
class, the content is as follows
@RestController public class HelloController { @RequestMapping("/hello") public String index() { return "Hello World"; } }
- Start the main program, open the browser to visit
http://localhost:8080/hello
, you can see the page outputHello World
Write unit test cases
打开src/test/
下的测试入口DemoApplicationTests
类。下面编写一个简单的单元测试来模拟http请求,具体如下:
@RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests { private MockMvc mvc; @Before public void setUp() throws Exception { mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build(); } @Test public void getHello() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().string(equalTo("Hello World"))); } }
使用MockServletContext
来构建一个空的WebApplicationContext
,这样我们创建的HelloController
就可以在@Before
函数中创建并传递到MockMvcBuilders.standaloneSetup()
函数中。
注意引入下面内容:
import static org.hamcrest.Matchers.equalTo; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Ok,跑起来,完全和预想的一样。至此已完成本章目标,通过Maven构建了一个空白Spring Boot项目,再通过引入web模块实现了一个简单的请求处理。
如此快速、开箱即用,爽到飞起。