Spring Boot 设计目的是用来简化新Spring 应用的初始搭建以及开发过程。他拥有嵌入的Tomcat,无需部署WAR 文件 , Spring Boot 并不是对Spring 功能上的增强,而是提供了一种快速使用Spring 的方式。可以通过SpringBoot官网 快速构建SpringBoot项目, 自动为你生成启动类以及相关文件等。
一、环境准备
- jdk1.8
- maven 3.3以上
- IDEA最好2018版本及以上
二、maven和pom.xml配置
1.maven设置:将配置设置成aliyun,下载速度快
<!-- 配置JDK版本 -->
<profile>
<id>jdk18</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
<!-- 当 nexus-aliyun 下不了的包,或许这个镜像能下,
才开放它,这个实在太慢,而且要把它放在首位,即 nexus-aliyun 之前,做过测试。
所以它的用途只有那么一瞬间,就是或许它能下载,可以通过 url 去查找确定一下
-->
<!-- <mirror>
<id>spring-libs-milestone</id>
<mirrorOf>central</mirrorOf>
<name>Spring Milestones</name>
<url>http://repo.spring.io/libs-milestone</url>
</mirror> -->
<!-- nexus-aliyun 首选,放第一位,有不能下载的包,再去做其他镜像的选择 -->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
<!-- 备选镜像,也是可以通过 url 去查找确定一下,
该镜像是否含有你想要的包,它比 spring-libs-milestone 快 -->
<mirror>
<id>central-repository</id>
<mirrorOf>*</mirrorOf>
typor<name>Central Repository</name>
<url>http://central.maven.org/maven2/</url>
</mirror>
2.项目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>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Spring Boot web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!-- Spring Boot maven依赖 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、显示Hello World
1.controller层
//@RestController:提供实现了REST API,可以服务JSON,XML或者其他。这里是以String的形式渲染出结果。
@RestController
public class HelloController {
//@RequestMapping:提供路由信息,”/“路径的HTTP Request都会被映射到hello方法进行处理。
@RequestMapping("/hello")
public String hello(){
return "Hello World" ;
}
}
2.开箱即用的启动应用类
//@SpringBootApplication:Spring Boot 应用的标识
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
3.启动DemoApplication,在浏览器输入http://localhost:8080/hello就可以看到Hello World。
四、修改端口和访问路径
1.修改application.properties配置文件
server.port=8081
server.servlet.context-path=/demo
2.在浏览器输入http://localhost:8081/demo/hello显示Hello World。