环境准备
- Java 8
java -version
#我使用的是openjdk
openjdk version "1.8.0_191-1-redhat"
...
- Maven 3
mvn -v
Apache Maven 3.5.4
...
创建Maven项目
- 执行mvn命令:
mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.sample -DartifactId=spring-boot-demo
-
查看新创建的Maven项目:
- 查看App类;
package com.mycompany.sample;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
- 查看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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.sample</groupId>
<artifactId>spring-boot-demo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring-boot-demo</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
以上是默认Maven项目的样子,test就先不介绍了。
将Maven项目变成Spring Boot项目
- 添加Spring Boot声明;
#在pom.xml的根节点添加以下内容:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/>
</parent>
- 增加Web支持(为后续开发API做准备);
#在pom.xml的dependencies点下添加以下内容:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
#此处不用再指定版本,默认跟spring-boot-starter-parent一致,即2.3.1.RELEASE,Spring Boot相关的依赖是一整套的。
#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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.sample</groupId>
<artifactId>spring-boot-demo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring-boot-demo</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/>
</parent>
<repositories>
<repository>
#此处可以使用国内Maven镜像,如aliyun镜像、清华大学镜像等
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
- 修改App类;
- 在App类上添加注解@SpringBootApplication;
- 在main方法中添加一行代码;
#整体代码
package com.mycompany.sample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
到此为止,我们已经建立了Spring Boot应用了,整个项目是可以运行的!
只不过在项目中我们还没有开发API,不能通过浏览器访问。
开发API
- 创建controller包,包名com.mycompany.sample.controller;
-
创建Controller,如HelloWorldController.java;
(注:controller一般就是指写API的地方;controller及其他的包、类一般放在App.java同级或下级目录下)
- 编写Controller逻辑;
package com.mycompany.sample.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author : dylanz
* @since : 06/29/2020
**/
@RestController
public class HelloWorldController {
@GetMapping("/sayHello")
public String sayHello() {
return "Hello World!";
}
@GetMapping("/sayHello/{name}")
public String sayHelloWithNameInPath(@PathVariable String name) {
return "Hello " + name;
}
@GetMapping("/hello")
public String sayHelloWithNameInRequestParam(@RequestParam("name") String name) {
return "Hello " + name;
}
}
启动项目,客户端请求API
- 启动Spring Boot应用;
-
IDEA中直接启动App.java;
(注:我们会看到,启动App类时,项目自动启动了tomcat,Spring Boot已经帮我们集成好tomcat了,多么美好的一件事情!!!)
- 浏览器中访问API;
- http://127.0.0.1:8080/sayHello
- http://127.0.0.1:8080/sayHello/dylan
-
http://127.0.0.1:8080/hello?name=dylan
码字不容易,点赞需积极
谢谢!!!