Spring Boot 2.X combat tutorial (4) first Spring Boot program

4 . The first Spring Boot program

This section describes how to develop a simple "the Hello World ! " Web applications.

We use Maven to build the project, because most of the IDE supports it.

4.1 Use Spring Initializr building

Spring Initializr provides a simple Web interface for custom projects.

item: item type ( Maven-Project , Gradle-Project )

Language: To use programming languages ( the Java , Groovy , or Kotlin )

l Spring Boot: Spring Boot version

Group L : Item group ID , the groupId Apache Maven referenced attributes.

Artifact L : the work  ID , the artifactId Apache Maven referenced attributes.

Name: The name of the project, it also determines Spring Boot application name.

Description: Project Description

Package name: root package of the project. If not specified,  the use of Group property values of

Packaging: Packaging project . You can generate a jar or war project .

l Java version: To use the Java version ( 1.8 , 11 )

. 4 . 2 the Spring the Boot Code

4.2.1 POM file

Maven pom.xml is used to build the project configuration .

You can run mvn package to test it .

You can import a project IDE (most modern Java IDE includes Maven built-in support).

For simplicity, you can use a plain text editor.

Recommended use Notepad ++

. 4 .2 .2 dependencies

We can view the current contents by running the following command:

$ mvn dependency:tree

Found that many dependencies, including Tomcat Web server and Spring Boot itself.

4 . 2. 3 class file

By default, Maven Compiler folder under src / main / java source code .

4 . 2 . 4  @RestController comment

@RestController is called stereotype annotations. It provides tips for the person reading the code, and for Spring provides tips for specific roles. In this case, our class is a Web @Controller , therefore Spring in processing incoming Web request will consider it. 

You need to import import org.springframework.web.bind.annotation *.;

4.2.5 @ RequestMapping comments

 

@RequestMapping notes provide " routing " information. It tells the Spring , with any / path HTTP request should be mapped to the home method. The  @RestController annotation tells Spring string so that the resulting returns directly to the caller.

. 4 . 2 . . 6 " main " method

Main method is the Java standard method agreed application entry point.

Main method is by calling the run delegate Spring guided SpringApplication class.

The revised SpringApplication class for the following:

package com.example.demo;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

 

import org.springframework.web.bind.annotation.*;

 

/**

 * Demo Application

 *

 * @Author big strong

 *

 */

@SpringBootApplication

@RestController

public class DemoApplication {

 

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

 

@RequestMapping("/")

String home() {

return "Hello World!";

}

 

}

4 . 3 operational program

Type from the root project directory mvn spring-boot: run to start the application.

If you open a Web browser localhost: 8080 , you should see the following output: the Hello World!

To properly exit the application, press Ctrl-c .

4 . 4 to create an executable Jar

To create an executable JAR , run from the command line mvn Package Penalty for , as follows: $ mvn Package Penalty for

To run the application, use the following java -jar command:

$ java -jar target/demo-0.0.1-SNAPSHOT.jar

As before, we want to exit the application, press Ctrl-c .

 

If in doubt, watch the video: https://ke.qq.com/course/428845

 

Guess you like

Origin www.cnblogs.com/daqiang123/p/11265705.html