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

一、进入Getting Started

https://spring.io/
点击Spring BootSpring Boot3入门快速体验-Developing Your First Spring Boot Application_第1张图片点击LEARN,再点击Reference Doc.
Spring Boot3入门快速体验-Developing Your First Spring Boot Application_第2张图片
再点击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应用程序):
Spring Boot3入门快速体验-Developing Your First Spring Boot Application_第3张图片
点击4. Developing Your First Spring Boot Application:

Spring Boot3入门快速体验-Developing Your First Spring Boot Application_第4张图片
Spring Boot3入门快速体验-Developing Your First Spring Boot Application_第5张图片

二、新建项目

新建一个Empty Project spring-boot-3-202376:
Spring Boot3入门快速体验-Developing Your First Spring Boot Application_第6张图片
Empty Project -spring-boot-3-202376下新建一个Module-boot3-01-demo:
Spring Boot3入门快速体验-Developing Your First Spring Boot Application_第7张图片

最后的项目结构
Spring Boot3入门快速体验-Developing Your First Spring Boot Application_第8张图片

三、Developing Your First Spring Boot Application

4.2. Setting up the project with Maven
所有springboot项目都必须继承自 spring-boot-starter-parent
Spring Boot3入门快速体验-Developing Your First Spring Boot Application_第9张图片
复制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。
Spring Boot3入门快速体验-Developing Your First Spring Boot Application_第10张图片
复制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
Spring Boot3入门快速体验-Developing Your First Spring Boot Application_第11张图片
4.5. Writing the Code
Spring Boot3入门快速体验-Developing Your First Spring Boot Application_第12张图片

#@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参考文档。

Spring Boot3入门快速体验-Developing Your First Spring Boot Application_第13张图片

Spring Boot3入门快速体验-Developing Your First Spring Boot Application_第14张图片
Spring Boot3入门快速体验-Developing Your First Spring Boot Application_第15张图片

在这里插入图片描述

你可能感兴趣的:(spring,boot,后端,java)