Introduction
Spring Boot is a basic configuration environment provided by Spring, which can be used to rapidly develop production-level products. It is especially suitable for developing microservice architecture, which saves a lot of configuration trouble. For example, when using Spring MVC, you only need to spring-boot-starter-web
add the dependency to the Maven dependency. In addition it has the following features:
- Create a standalone Spring project
- Built-in Tomcat, Jetty, Undertow
- Initial POM configuration file to simplify Maven configuration
- Auto-configure Spring as much as possible
- Provides production environment features such as statistics, health checks and external configuration
- No XML configuration and code generation required
Create a Spring Boot application
- Development environment: IntelliJ, JDK 1.8
- Project source code Gitee
First create a maven project in IntelliJ:
- GroupID: cn.zxuqian
- ArtifactId: helloworld
After the creation is complete, the lower right corner of IntelliJ will prompt to automatically import the Maven configuration. Select Enable Auto-Import to start the automatic import. Then add the following code to pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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>
Tags add spring-boot-starter-web
dependencies, namely Spring MVC and the associated runtime environment. spring-boot-maven-plugin
The plugin provides a set of maven run goals that can easily package, deploy and run applications. After waiting for a while, Maven automatically downloads the dependencies and you can start writing the code.
Create the first controller
src/main
Create a new package under cn.zxuqian.controllers
and create a new class in it called HelloController
and add the following code:
package cn.zxuqian.controllers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Hello World!";
}
}
@RestController
Mark this class as a Rest controller and ready to handle Rest requests.@RequestMapping("/")
i.e. this method handles root path requests, eghttp://localhost:8080/
.index
The method returns String type, that is, the response returns string data, here is "Hello World".
Create the Application class
Create a class under the cn.zxuqian
package Application
and add the following code:
package cn.zxuqian;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@SpringBootApplication
Indicates that this class is the startup class for Spring Boot applications.
run the app
Select in the right tab of IntelliJ Maven Projects
, then expand Plugins-->spring-boot , select spring-boot:run
Target . After the startup is successful, access it in the browser and http://localhost:8080
see it Hello World!
successful.
The article comes from my blog: http://zxuqian.cn/spring-boot-get-started/ , welcome to visit.