Background of 0.

Start learning Spring Boot.

spring boot

1. Introduction

Spring Boot is a best practice for building applications on top of the Spring framework. As the name implies, it aims to quickly build a Spring application in a “boot now” manner.

Spring Boot makes it easy to create standalone, production-grade Spring-based applications. It takes a unique look at the Spring platform and third-party libraries, making it easy to build Spring applications with less hassle.

features

  • Create a stand-alone Spring application
  • Embed directly into Tomcat, Jetty, or Undertow(no need to deploy WAR files)
  • Provide unique “starter” dependencies to simplify build configurations
  • Automatically configure Spring and third-party libraries whenever possible
  • Provides production-ready features such as metrics, health checks, and externalized configurations
  • No code generation is required, and no XML configuration is required

2 Before WE start

Before you start, you need to make sure you have these ready:

  • 1) Install the JDK
  • 2) Configure JDK environment variables
  • 3) Install IDE development tools, such as IntelJ IDEA here

3. Operation example

Let’s start building our first SpringBoot-based HelloWorld application. It is expected to be divided into such steps:

  • (1) Create a SpringBoot project
  • (2) Import the project into Idea development tool
  • (3) Edit the code
  • (4)

These are described below

3.1 Creating a SpringBoot Project

There are two optional ways to create a SpringBoot project

    1. Use the boot tool provided on the official website at start.spring. IO /
    1. Use the Create project wizard in IDEA.

Method 1: Use the boot tool provided by the official website

  1. Go to start.spring. IO /

  2. Select configurations for some projects

    \

    image.png

  3. Click the GENERATE button.

After the above steps are complete, you will get a project file package.

Method 2: Use the Create project wizard in IDEA

  1. Start the IDEA

  2. Select File –> New –> Project

  3. Select Spring Initializr, which is a project boot, and click OK.

    \

    spring initializr

  4. Fill in the blanks and click Next until you’re done.

After the above steps are complete, you will get a project file package.

3.2 Import the project to IDEA development tool

After the preceding steps are completed, the project file package can be used only after being imported into IDEA. Be sure to Import Project before using it.

Method: Start IDEA, select Import Project, and select the Project file path just generated.

After importing, IDEA automatically initializes and configures the environment required for development, and downloads dependent packages.

3.3 Editing Code

Open the IDEA development tools, in the project file paths SRC/main/Java/com/example/demo (according to you create the project, the name may be different) find DemoApplication. Java file, edit it.

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {
  
    
    public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
    }
    
    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
    return String.format("Hello %s!", name);
    }
  
}   
Copy the code

Code description: defines a Hello method, the user handles url mapping, HTTP requests from/Hello will be handled here.

@getMapping (“/hello”) This is an annotation that defines a mapping of the get method.

RestController, this annotation, defines this class as a controller.

SpringApplication.run(DemoApplication.class, args); This line of code launches a Spring Boot container application.

3.4 start

Spring Boot has a Tomcat embedded so it can be started directly. On the console enter:

./mvnw spring-boot:run
Copy the code

Hopefully, a Web service will be launched. We can enter the following url in the browser to access.

http://localhost:8080/hello
Copy the code

Browser access:

image.png

4. Configuration and problems

Configure the boot option of IDEA

IDEA has a convenient launch button that helps us save time. Let’s configure it, click the Edit Configurations button

image.png

Select Spring Boot and specify Main Class.

image.png

To complete.

5. Problems encountered

Cannot access org.springframework….

New import project, error message:

 Cannot access org.springframework.context.ConfigurableApplicationContext
Copy the code

Delete the xxx. imL automatically generated by the import project and restart it.

6. Reference

Spring. IO/projects/sp…

spring.io/quickstart

start.spring.io/

www.jetbrains.com/help/idea/s…