[Springboot full tutorial] The first Spring Boot project

When you first contacted and learned the Spring framework, did you retreat because of its complicated configuration? When you used the Spring framework for the nth time, did you feel bored with a bunch of pasted configurations? Then you might as well try to use Spring Boot to make it easier for you to use and build Spring applications more easily and quickly! 

Spring Boot makes our Spring applications lighter. For example: you can only rely on a Java class to run a Spring reference. You can also package your application as a jar and run your Spring Web application by using java -jar.

The main advantages of Spring Boot:

  • Get started faster for all Spring developers
  • Out of the box, various default configurations are provided to simplify project configuration
  • Embedded container simplifies web projects
  • No redundant code generation and XML configuration requirements 

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, you have a preliminary understanding of Spring Boot and experience its simple structure and rapid development.

System Requirements:

  • Java 7 and above
  • Spring Framework 4.1.5 and above

This article adopts Java 1.8.0_73, Spring Boot 1.3.2 to debug and pass.

Use Maven to build the project

1. Pass

SPRING	INITIALIZR

Tools to generate basic items

i. Visit: http://start.spring.io/

ii. Choose to build the tool Maven Project, Spring Boot version 1.3.2 and some basic project information, you can refer to the following

As shown in the picture 

SPRING INITIALIZR

iii. Click Generate Project to download the project compressed package 

2. Unzip the project package and use the IDE to 

maven

Project import to 

IntelliJ	IDEA	14 

For example:

i. Select File –> New –> Project from Existing Sources... from the menu

ii. Select the unzipped project folder and click OK

iii. Click Import project from external model and select Maven, click Next to the end.

iv. If your environment has multiple versions of JDK, please note that when choosing Java SDK, please choose a version above Java 7 

Project structure analysis

Project structure 

The creation of the basic project is completed through the above steps. As shown in the figure above, there are three files in the basic structure of Spring Boot (the specific path is based on all the differences in the Group filled in when the user generates the project): 

  • Program entry under src/main/java: Chapter1Application
  • Configuration file under src/main/resources: application.properties
  • Test entry under src/test/: Chapter1ApplicationTests

Both 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.

Introduce web module

The current pom.xml content is as follows, only two modules are introduced:

spring-boot-starter: core modules, including automatic configuration support, logging and YAML

spring-boot-starter-test: Test modules, including JUnit, Hamcrest, Mockito 

<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 import 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> 

Write HelloWorld service

  • Create a package named com.didispace.web (modified according to actual conditions)
  • Create the HelloController class with the following content 
@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 output Hello

World 

Write unit test cases

Open the test entry Chapter1ApplicationTests class under src/test/. Write a simple unit test to simulate the http request, as follows: 

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes	=	MockServletContext.class)
@WebAppConfiguration
public	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.APPLICATI
ON_JSON))
																.andExpect(status().isOk())
																.andExpect(content().string(equalTo("Hello	World")));
				}
} 复制代码

Use MockServletContext to build an empty WebApplicationContext, so we create

The HelloController can be created and passed in the @Before function

To the MockMvcBuilders.standaloneSetup() function. 

Pay attention to the introduction of 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.con
tent;
import	static	org.springframework.test.web.servlet.result.MockMvcResultMatchers.sta
tus;复制代码

So far, the goal has been completed, a blank Spring Boot project has been built through Maven, and a simple request processing has been implemented by introducing a web module.

Public number: Java senior architect, enter " interview questions " to get 500 real interview questions from major manufacturers!

Guess you like

Origin blog.csdn.net/qq_17010193/article/details/115315317