SpringBoot2.x basic articles: develop your first SpringBoot application

SpringBoot2.x basic articles: develop your first SpringBoot application

Knowledge changes my destiny, code makes me happy, 2020 continue to walk in the open source world like and then

watch, develop a habit

to give me a Star, click to learn about the SpringBoot-based componentized interface service landing solution

This article is the beginning of 2020. I hope it can bring you a different reading experience and a clear reading idea.

I have been writing SpringBootarticles related to the use of technical points since 2017. The original version is still 1.5.2because the SpringBootlarge and small versions are released too fast, SpringBootthere are certain differences between the old version of the article and the new version of the application, in order to make everyone faster The SpringBoot 2.xcore technical points of the introductory learning version will be updated successively to use articles of some basic knowledge points. The naming format of basic articles:SpringBoot2.x ...

Development environment

SpringBoot2.xBased version is Java8written in, due to internal use to many new features such as: lambda, interface default..., so it is necessary to develop the local environment java8support.

Not only that, SpringBootthe default Mavenmethod is used when building the project , so the local development environment also needs to configure Mavenenvironment variables.

~ java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode) 

~ mvn -version          
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home:/Users/yuqiyu/soft/apache-maven-3.6.3
Java version: 1.8.0_231, vendor: Oracle Corporation, runtime:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.3", arch: "x86_64", family: "mac" 

If you prefer to use the Gradleway to build the project, then local Gradleenvironment variables should be supported.

The version restrictions of the build tool are shown in the following table:

Build tool version
5.x or 6.x

New project

SpringBootThere are many ways to create a new application:

  1. Use IDEA's built-in Spring Initializrcreation (File -> New -> Project -> Spring Initializr)
  2. Create a basic Maven project, modify and pom.xmladdspring-boot-parent
  3. After visiting start.spring.io to select dependencies, generate the project and download

I generally use the first method, which is relatively fast. IDEAInternally , the built files are downloaded to the local and decompressed through start.spring.iozip , so you need to connect to the Internet to create a project.

The pom.xmlfile content of the new project is as follows:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <relativePath/> <!-- lookup parent from repository -->
  <description>Demo project for Spring Boot</description>


    <!-- ...-->



The newly created application will be automatically spring-boot-parentused , and then we have some default resource configuration, default dependency version, default plug-in configuration, etc.

Add dependency

When we need project support SpringMvc, modify the pom.xmlfile and add the following dependencies:


The spring-boot-starter-webmain purpose of adding dependencies is to demonstrate Hello Worldoutput.

Sample code

To complete our application, we need to create a Javafile. By default , the source code in the directory Mavenwill be compiled src/main/java. We can create it in this directory packageto classify the source code. Let s create a HelloExample.javasample source code named The contents of the file are as follows:

package org.minbox.chapter.developing.first.application;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

 * Hello Example
 * @author  
public class HelloExample {

    public String hello() {
        return "hello world!";

Run example

So far, our newly created application should be able to work. Because the application parentis spring-boot-parent, it has a built-in environment support that can be run . The application can be run directly through the command line. When we are at the root of the application Enter the command under the directory:

~ developing-first-application   mvn spring-boot:run 

By Mavendownloading the relevant dependencies to the local default dependency repository (~/.m2/repository), the project will run automatically after the compilation is passed, and the console output is as follows:

 .   ____          _            __ _ _
/\/___'_ __ _ _(_)_ __  __ _///\
( ( )\___ | '_ | '_| | '_//_` |///\
\\/ ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, |////
 :: Spring Boot ::  (v2.2.4.RELEASE)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.222 seconds (JVM running for 6.514) 

When you see the above content output in the console, our application has run successfully, and you can see the following output when you visit http://localhost:8080 in the browser :

hello world! 

If you want to exit the running application, use it Crtl + C.

Author blog

The use of open-source framework ApiBoot help you become an architect Api interface services