Spring Boot3 Getting Started Quick Experience-Developing Your First Spring Boot Application

1. Enter Getting Started

https://spring.io/Click
Spring Boot,insert image description here click LEARN , and then click Reference Doc . :
insert image description here
Then click Getting Started Introducing Spring Boot, System Requirements, Servlet Containers, Installing Spring Boot, and Developing Your First Spring Boot Application (Introducing Spring Boot, System requirements, Servlet container, installing Spring Boot and developing your first Spring Boot application):
insert image description here
Click 4. Developing Your First Spring Boot Application :

insert image description here
insert image description here

2. New projects

Create a new Empty Project spring-boot-3-202376 : Create a new Module - boot3-01-demo
insert image description here
under Empty Project - spring-boot-3-202376 :
insert image description here

Final project structure :
insert image description here

三、Developing Your First Spring Boot Application

4.2. Setting up the project with Maven
All springboot projects must inherit from spring-boot-starter-parent
insert image description here
Copy the code in the parent tag to the pom.xml in the spring-boot-3-202376 \boot3-01-demo directory :

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.1</version>
    </parent>

    <!-- Additional lines to be added here... -->

</project>


4.4. Scenario starter for Adding Classpath Dependency
web development Focus: Provide an optional starter to simplify application integration.
Scenario starter (starter): web, json, mail, oss (object storage), asynchronous, scheduled tasks, cache...
A dependency is prepared for each scenario; web-starter, mybatis-starter.
insert image description here
Copy the dependencies tag to the pom.xml in the spring-boot-3-202376 \boot3-01-demo directory :

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

After inheriting spring-boot-starter-parent and introducing the starter starer related to web development, the pom.xml at this time :
insert image description here
4.5. Writing the Code
insert image description here

#@RestController是一个合成注解,由@ReponseBody@Controller合成,具有两个注解的功能。
@ReponseBody:给浏览器返回纯文本。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class MyApplication {
    
    

    @RequestMapping("/")
    String home() {
    
    
        return "Hello World!";
    }

    public static void main(String[] args) {
    
    
        SpringApplication.run(MyApplication.class, args);
    }

}

Click to run MyApplication :
4.5.1. The @RestController and @RequestMapping Annotations
4.5.2. The @SpringBootApplication Annotation

 @RestController and @RequestMapping 注解
关于我们的MyApplication类是 @Rest控制器。这称为刻板印象注释。它为阅读代码的人提供了提示,并为Spring提供了类扮演特定角色
的提示。在这种情况下,我们的类是一个web@Controller,所以Spring在处理传入的web请求时会考虑它。

这个@RequestMapping注释提供了“路由”信息。它告诉Spring /路径应映射到home方法 @Rest控制器注释告诉Spring将结果字符串直接
返回给调用者。



@SpringBootApplication 
第二个类级注释是@SpringBootApplication。此注释称为元注解,它结合了@SpringBootConfiguration ,
@enableautoconfiguration@ComponentScan .
其中,我们最感兴趣的注释是@EnableAutoConfiguration .@enableautoconfiguration告诉Spring Boot根据您添加的jar依赖项
“猜测”您想要如何配置Springspring-boot-starter-web添加了TomcatSpring MVC,自动配置假定您正在开发web应用程序并相应
地设置Spring

The @RestController and @RequestMapping annotations are Spring MVC annotations (they are not specific to Spring Boot). See the MVC section in the Spring Reference Documentation for more details.

Translation:
@RestController and @RequestMapping are Spring MVC annotations (they are not Spring Boot-specific) MVC segments For more details, please refer to the Spring reference documentation.

insert image description here

insert image description here
insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/qyfx123456/article/details/131588193