本系列文章将从一个简略的 HelloWorld 我的项目开始,讲述如何一步步搭建一个残缺的 Spring Web 我的项目。必要时,将翻阅相干的源代码,剖析其中的实现细节。
版本阐明:
- 所有的我的项目代码均构建在 Spring Boot 2.7.2 之上。
学习前提:
- 文章中波及到的代码次要构建在 Java 语言之上,因而浏览之前把握根本的 Java 语言根底是必须的;
- 我的项目代码以 Maven 作为项目管理工具,开始之前你该当对 Maven 的基础知识有所理解。
Maven 我的项目
我的项目构造
开始之前,咱们先温习一下 Maven 我的项目的根本内容,它的目录构造如下:
.├── pom.xml└── src ├── main │ ├── java │ └── resources └── test ├── java └── resources
pom.xml 文件次要用于定义我的项目的依赖,main 目录次要用于寄存我的项目的代码和配置,test 目录次要用于寄存测试的代码和配置。
pom.xml
上面是一个简略的 pom.xml 文件:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1.0-SNAPSHOT</version> <name>my-app</name> <url>http://www.example.com</url></project>
modelVersion 指定 pom 文件合乎哪个版本的描述符,groupId、artifactId、version 用来独特标识一个惟一的 jar 包,name 和 url 为描述性内容。
疾速开始
搭建一个 Spring Web 我的项目,只须要简略的几步:
1. 继承 Spring Boot 我的项目
spring-boot-starter-parent
预约义了各种 starter
的版本,继承并指定了该项目标版本,咱们在引入 spring-boot-starter-web
的时候,就不须要去指定它的版本了。
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.2</version> <relativePath/></parent>
2. 引入 Spring Web 依赖
spring-boot-starter-web
引入了 Spring Web 我的项目所须要的全副依赖。因而,咱们在引入 Spring Web 依赖的时候,只须要简略的申明:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>
最终 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.2</version> <relativePath/> </parent> <groupId>org.susamlu.springweb</groupId> <artifactId>spring-web-helloworld</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-web-helloworld</name> <description>Demo project for Spring Web</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies></project>
3. 编写 Controller 代码
通过 @RestController 申明接口类,由此,咱们能够在该类中定义 RESTful 格调的接口办法。应用 @GetMapping 指定接口的门路,应用 @RequestParam 指定接口接管的申请参数。
package org.susamlu.springweb.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloWorldController { @GetMapping("/hello") public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { return String.format("Hello %s!", name); }}
4. 编写启动类
应用 @SpringBootApplication 指定我的项目的启动类,在 main() 办法中执行 SpringApplication.run() 办法,至此,HelloWorld 我的项目的代码就全副编写完了。
package org.susamlu.springweb;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class HelloWorldApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldApplication.class, args); }}
5. 运行启动类
运行启动类,在浏览器中输出:http://localhost:8080/hello?name=小穆
,即可看到如下成果: