创建新模块,选择Spring初始化,并配置模块相关基础信息
选择当前模块需要使用的技术集
开发控制器类
运行自动生成的Application类
创建新模块
在IDEA下创建一个新模块,选择Spring Initializr,用来创建SpringBoot工程
选中 Web,然后勾选 Spring Web,由于我们需要开发一个 web 程序,使用到了 SpringMVC 技术,所以按照下图红框进行勾选
点击创建后就创建成功
在创建好的工程中不需要创建配置类
创建好的项目会自动生成其他的一些文件,而这些文件目前对我们来说没有任何作用,所以可以将这些文件删除。
可以删除的目录和文件如下:
.mvn
.gitignore
HELP.md
mvnw
mvnw.cmd
创建Controller
在com.blog.controller包下创建BookController,代码如下
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println("get id ==> " + id);
return "hello,spring boot!";
}
}
启动服务器
运行 SpringBoot 工程不需要使用本地的 Tomcat 和 插件,只运行项目 com.blog 包下的 Application 类,我们就可以在控制台看出如下信息
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.3)
2022-09-14 20:01:16.764 INFO 10956 --- [ main] c.b.Springboot01QuickstartApplication : Starting Springboot01QuickstartApplication using Java 1.8.0_321 on Kyle with PID 10956 (D:\Study\SpringBoot\springboot_01_quickstart\target\classes started by Kyle in D:\Study\SpringBoot)
2022-09-14 20:01:16.770 INFO 10956 --- [ main] c.b.Springboot01QuickstartApplication : No active profile set, falling back to 1 default profile: "default"
2022-09-14 20:01:17.891 INFO 10956 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-09-14 20:01:17.900 INFO 10956 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-09-14 20:01:17.900 INFO 10956 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-09-14 20:01:18.060 INFO 10956 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-09-14 20:01:18.060 INFO 10956 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1211 ms
2022-09-14 20:01:18.428 INFO 10956 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-09-14 20:01:18.437 INFO 10956 --- [ main] c.b.Springboot01QuickstartApplication : Started Springboot01QuickstartApplication in 2.305 seconds (JVM running for 3.861)
进行测试
依旧是使用PostMan来测试,发送GET请求访问localhost:8080/books/9527
可以看到响应回来的结果hello,spring boot!
同时控制台也输出了get id ==> 9527
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>
<!--指定了一个父工程,父工程中的东西在该工程中可以继承过来使用-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.blog</groupId>
<artifactId>springboot_01_quickstart</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--JDK 的版本-->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--该依赖就是我们在创建 SpringBoot 工程勾选的那个 Spring Web 产生的-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--这个是单元测试的依赖,我们现在没有进行单元测试,所以这个依赖现在可以没有-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!--这个插件是在打包时需要的,而这里暂时还没有用到-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
##Spring和SpringBoot对比
- 坐标
Spring 程序中的坐标需要自己编写,而且坐标非常多
SpringBoot 程序中的坐标是我们在创建工程时进行勾选自动生成的 - web3.0配置类
Spring 程序需要自己编写这个配置类。这个配置类我们之前编写过,肯定感觉很复杂
SpringBoot 程序不需要我们自己书写 - 配置类
Spring/SpringMVC 程序的配置类需要自己书写。
而 SpringBoot 程序则不需要书写。