SpringBoot 从入门到光头 —— 第二章 SpringBoot2 入门
1. 系统要求
- Java8 以上
- Maven 3.3 以上
- IDEA 2019 以上
1.1. Maven 配置修改
maven/conf/settings.xml
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>jdk-1.8</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>
</profiles>
2. 编写一个 HelloWorld
需求:浏览发送 /hello
请求,响应 Hello,SpringBoot2
2.1. 创建一个 Maven 工程
2.2. 导入依赖坐标
pom.xml
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.5.6</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2.3. 创建主程序
com.yourname.boot.MainApplication
/**
* Main program class
*
* <br/><code>@SpringBootApplication</code>: This is a SpringBoot application
*
* @author gregPerlinLi
* @since 2021-10-25
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
2.4. 编写业务
com.yourname.boot.controller.HelloController
/**
* @author gregPerlinLi
* @since 2021-10-25
*/
@RestController
public class HelloController {
@RequestMapping("/hello")
public String handle01(){
return "Hello, SpringBoot2";
}
}
2.5. 测试
直接运行 main
方法即可
2.6. 简化配置
application.properties
server.port=8080
2.7. 简化部署
pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后将项目打包为 jar
包,直接在目标服务器执行即可
注意⚠️:在 CMD 运行时需要关闭快速编辑模式