Spring Boot3入门快速体验-Developing Your First Spring Boot Application

一、进入Getting Started

https://spring.io/
点击Spring Boot在这里插入图片描述点击LEARN,再点击Reference Doc.
在这里插入图片描述
再点击Getting Started Introducing Spring Boot, System Requirements, Servlet Containers, Installing Spring Boot, and Developing Your First Spring Boot Application(介绍Spring Boot、系统要求、Servlet容器、安装Spring Boot和开发您的第一个Spring Boot应用程序):
在这里插入图片描述
点击4. Developing Your First Spring Boot Application

在这里插入图片描述
在这里插入图片描述

二、新建项目

新建一个Empty Project spring-boot-3-202376:
在这里插入图片描述
Empty Project -spring-boot-3-202376下新建一个Module-boot3-01-demo:
在这里插入图片描述

最后的项目结构
在这里插入图片描述

三、Developing Your First Spring Boot Application

4.2. Setting up the project with Maven
所有springboot项目都必须继承自 spring-boot-starter-parent
在这里插入图片描述
复制parent标签里的代码到spring-boot-3-202376\boot3-01-demo目录下的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 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. Adding Classpath Dependencie
web开发的场景启动器
重点:提供可选的starter,简化应用整合。
场景启动器(starter):web、json、邮件、oss(对象存储)、异步、定时任务、缓存…。
为每一种场景准备了一个依赖; web-starter、mybatis-starter。
在这里插入图片描述
复制dependencies 标签,到spring-boot-3-202376\boot3-01-demo目录下的pom.xml中:

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

继承spring-boot-starter-parent和引入web开发相关的启动器starer后,此时的pom.xml
在这里插入图片描述
4.5. Writing the Code
在这里插入图片描述

#@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);
    }

}

点击运行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.

译文:
@RestController and @RequestMapping 是Spring MVC注释(它们不是特定于Spring Boot的)MVC段有关更多详细信息,请参阅Spring参考文档。

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qyfx123456/article/details/131588193