HelloWorld
# 30.HelloWorld
介绍了这么多原理,开始动手吧!注意要检查Java和Maven的版本
# 需求
浏览器发送/hello请求,响应 Hello
# 配置Maven
为了方便,我们可以修改下Maven的配置文件:添加阿里镜像,并且配置JDK为1.8
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</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
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 创建Maven项目
然后我们修改pom.xml文件,添加父工程:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
2
3
4
5
这在文档中也能看到:
# 添加依赖
下一步就是添加依赖了。目前我们是要开发web的场景,只需添加如下依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2
3
4
5
6
就能完成SpringMVC的引入,然后就可以开发web了。
也可以看到有引入很多web相关的依赖:
# 主程序
我们写一个类,用来运行我们的整改SpringBoot项目:
package com.peterjxl.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 主程序类
* @SpringBootApplication 这是一个SpringBoot应用程序
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
// 启动SpringBoot应用程序
SpringApplication.run(MainApplication.class, args);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
注解和main方法里的内容是固定写法,知道就行。然后我们就可以编写业务逻辑了,不像之前要写一大堆配置。
# 编写业务逻辑
package com.peterjxl.boot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController // @RestController = @Controller + @ResponseBody
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello, Spring Boot 2!";
}
}
2
3
4
5
6
7
8
9
10
11
12
13
这里用的都是SpringMVC里的注解,@RestController
是SpringBoot提供的注解,其等价于@Controller + @ResponseBody。我们可以看@RestController的源码,其实就是这两个注解加起来而已。
这个类主要是返回一个字符串Hello, Spring Boot 2!
给浏览器。
# 运行
接下来我们直接运行main方法即可,如果放在以前,还得整合Tomcat等。可以看到有输出日志,并且告诉我们监听了8080端口:
我们访问localhost:8080/hello/ (opens new window),可以看到有返回:
# 简化配置
SpringBoot帮我们简化了配置,例如我们之前如果想要改端口号,还得修改Tomcat的配置文件;
而SpringBoot整合了Spring生态圈的很多框架,我们可以在一个配置文件里。
我们新建resources/application.properties文件,配置端口号:
server.port=9999
然后我们重启,就可以访问9999端口了
至于具体能写哪些配置呢?可以在文档上看Application properties:
# 简化部署
之前我们部署时,需要在服务器上安装Tomcat,然后讲应用打成war包,然后在放到Tomcat上;
而使用了SpringBoot,我们就可以将应用打成一个可执行的jar包,然后直接在服务器上运行!
We finish our example by creating a completely self-contained executable jar file that we could run in production. Executable jars (sometimes called “fat jars”) are archives containing your compiled classes along with all of the jar dependencies that your code needs to run.
要创建该jar包,需要使用SpringBoot提供的插件:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2
3
4
5
6
7
8
文档也有说明:
我们运行下打包:
然后在命令行里运行该jar包:
java -jar LearnSpringBoot-1.0-SNAPSHOT.jar
然后也能在浏览器上访问。
其实原理就是,该jar包含了我们所有需要用到的jar包(lib目录下),并且包含了Tomcat的依赖。我们可以用压缩软件打开jar包:
# 常见错误
如果遇到在命令行启动,卡住的情况:建议取消掉cmd的快速编辑模式(该模式下右键单击即可快速复制和粘贴内容)
# 源码
已将本文源码上传到Gitee (opens new window)或GitHub (opens new window) 的分支demo1,读者可以通过切换分支来查看本文的示例代码