Brief introduction
Spring boot is a set of basic configuration environments provided by spring that can be used to quickly develop products at the production environment level. Especially suitable for the development of micro-service architecture, save a lot of configuration trouble. For example, when you use spring MVC, you simply spring-boot-starter-web
add dependencies to maven dependencies. In addition, it has the following features:
- Create a separate spring project
- Built-in Tomcat, Jetty,undertow
- Initial POM configuration file to simplify MAVEN configuration
- Automatically configure spring as much as possible
- Provides production environment functions such as statistics, health checks and external configuration
- No need for XML configuration and code generation
Create a Spring Boot app
- Development environment: IntelliJ, JDK 1.8
- Project Source Code Gitee
First create a MAVEN project in IntelliJ:
- GroupID:cn.zxuqian
- Artifactid:helloworld
After creation, the IntelliJ will prompt you to import the Maven configuration automatically, and select Enable Auto-import to start the automatic import. Then add the following code to the Pom.xml:
<?xmlVersion= "1.0" encoding= "UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Cn.zxuqian</groupId> <artifactId>HelloWorld</artifactId> <version>1.0-snapshot</version> <parent> <groupId>Org.springframework.boot</groupId> <artifactId>Spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>Org.springframework.boot</groupId> <artifactId>Spring-boot-starter-web</artifactId> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>Org.springframework.boot</groupId> <artifactId>Spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
<dependencies>
The tag adds spring-boot-starter-web
dependencies, namely Spring MVC and the associated runtime environment. The spring-boot-maven-plugin
plugin provides a set of Maven run targets that can be easily packaged, deployed, and run. Wait a moment. maven automatically downloads dependencies and then can write on the handwritten code.
Create the first controller
src/main
under Create a new package cn.zxuqian.controllers
and create a new class in it, name HelloController
and add the following code:
package cn.zxuqian.controllers;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublicclass HelloController { @RequestMapping("/") publicindex() { return"Hello World!"; } }
@RestController
Mark this class as a rest controller and be ready to handle rest requests.
@RequestMapping("/")
That is, this method handles a root-path request, such as http://localhost:8080/
.
index
The method returns the string data, which is "Hello world".
Creating the Application Class
cn.zxuqian
create the class under the package Application
and add the following code:
package cn.zxuqian;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublicclass Application { publicstaticvoidmain(String[] args) { SpringApplication.run(Application.class, args); }}
@SpringBootApplication
Indicates the startup class for this type of Spring boot app.
Run the App
Select the Right tab in IntelliJ Maven Projects
, and then expand plugins-->spring-bootto select the spring-boot:run
target. After the successful launch, the access seen in the browser http://localhost:8080
Hello World!
is successful.
Article from my blog: http://zxuqian.cn/spring-boot-get-started/, welcome to visit.
Spring Boot 2.0.1 Getting Started tutorial