Quick Start
The main objective of this chapter is to build the Spring Boot Foundation project and implement a simple HTTP request processing, which gives a preliminary understanding of spring boot and experiences 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
debugging passes.
Building projects with Maven
SPRING INITIALIZR
creating basic projects with tools
- Access:
http://start.spring.io/
- Select the Build tool
Maven Project
, Spring boot version, 1.3.2
and some engineering basics to refer to the spring INITIALIZR
- Click to
Generate Project
download the project compression package
- Unzip the project package and use the IDE to
Maven
import the project as an IntelliJ IDEA 14
example:
- menu, select
File
–> New
–>Project from Existing Sources...
- Select the extracted Items folder and click
OK
- Click
Import project from external model
and select Maven
, and click to the Next
end.
- If you have multiple versions of the JDK in your environment, please select the
Java SDK
above version when you choose Java 7
Project Structure Analysis
Project structure
The following steps complete the creation of the base project, as shown in the Spring boot infrastructure with a total of three files (the exact path varies according to the group all the differences that the user fills in when the project is generated):
src/main/java
Under the program entry:Chapter1Application
src/main/resources
The following configuration file:application.properties
src/test/
Test entry under:Chapter1ApplicationTests
Both the generated Chapter1Application
and Chapter1ApplicationTests
class can run directly to start the currently created project, and because the project is currently not mated to any data access or Web modules, the program ends up running after the spring has finished loading.
Introducing the Web Module
The current pom.xml
content is as follows, with only two modules introduced:
spring-boot-starter
: core modules, including automatic configuration support, logs, 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 introduce a Web module, you need to add a spring-boot-starter-web
module:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-web</artifactid></dependency>
- Writing HelloWorld Services
- Create
package
a named com.didispace.web
(modified according to the actual situation)
- Create
HelloController
a class with the following content
@RestController Public class Hellocontroller { @RequestMapping ("/hello") public String Index () { return "Hello World";} }
@RunWith (Springjunit4classrunner.class) @SpringApplicationConfiguration (Classes= Mockservletcontext.class) @WebAppConfiguration Public classchapter1applicationtests {PrivateMOCKMVC MVC; @Before Public voidSetUp ()throwsException {MVC= Mockmvcbuilders.standalonesetup (NewHellocontroller ()). build (); } @Test Public voidGethello ()throwsException {mvc.perform (Mockmvcrequestbuilders.get ("/hello"). Accept (Mediatype.application_json)). Andexpect (Status (). IsOk ()). Andexpect (Content (). s Tring (Equalto ("Hello World"))); } }
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;
Source source technical support for complete projects 1791743380
Spring Cloud Spring Boot mybatis distributed micro-service Cloud Architecture (i) Quick start