Introduction
When you first came into contact with and learned about the Spring framework, did you retreat because of its complicated configuration? When you use Spring framework the nth time, do you feel a little bored with a bunch of repeatedly pasted configurations? Then you might as well try using Spring Boot to make it easier to get started and build Spring applications more easily and quickly!
Spring Boot makes our Spring applications lighter. For example: You can just rely on a Java class to run a Spring reference. You can also package your app as a jar and run your Spring web app by using java -jar.
Key advantages of Spring Boot:
Quick Start
The main goal of this chapter is to complete the construction of the Spring Boot basic project and implement a simple Http request processing. Through this example, we have a preliminary understanding of Spring Boot and experience its simple structure and fast development features.
System requirements:
This article uses Java 1.8.0_73 and Spring Boot 1.3.2 to debug and pass.
Build a project using Maven
1. Generate basic projects through SPRING INITIALIZR tool
Visit: http://start.spring.io/
Select the build tool Maven Project, Spring Boot version 1.3.2 and some basic engineering information, please refer to the SPRING INITIALIZR as shown in the figure below.
Click Generate Project to download the project compression package
2. Unzip the project package and import it with the Maven project using the IDE, taking IntelliJ IDEA 14 as an example:
Project structure analysis
The basic project was created through the above steps. As shown in the figure above, the infrastructure of Spring Boot has three files in total (the specific path is based on all the differences in the Group filled in when the user generates the project):
The generated Chapter1Application and Chapter1ApplicationTests 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.
Introducing Web Modules
The current pom.xml content is as follows, and only two modules are introduced:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency></dependencies>
To introduce the web module, you need to add the spring-boot-starter-web module:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>
Writing HelloWorld Services
@RestControllerpublic class HelloController { @RequestMapping("/hello") public String index() { return "Hello World"; }} Start the main program, open the browser and visit http://localhost:8080/hello, and you can see the page output Hello World
Writing unit test cases
Open the test entry under src/test/ Chapter1ApplicationTests class. Below is a simple unit test to simulate http requests, as follows:
@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = MockServletContext.class)@WebAppConfigurationpublic class Chapter1ApplicationTests { 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"))); }}Use MockServletContext to build an empty WebApplicationContext so that the HelloController we create can be created in the @Before function and passed to the MockMvcBuilders.standaloneSetup() function.
Pay attention to introducing the following content to make the status, content, and equalTo functions available
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;
The goal has been completed so far, a blank Spring Boot project was built through Maven, and a simple request processing was implemented by introducing a web module.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.