Spring boot学习之编写Hello world (二)
1、打开IDE,点击 file -> new -> project,然后出现如下图
2、点 Spring Initializr,然后点 next ,如果点 next 一直出现个进度条,那就回来把 https 改成 http再试试,如果还是不行,那你就要学习一下怎么科学上网了。
- Name:项目名
- Group:the unique identifier of the organization or group that created the project (GroupId是项目组织唯一的标识符,实际对应Java的包的结构,是main目录里java的目录结构)
- Artifact:unique base name of the primary artifact being generated by this project (ArtifactId是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称)
- Description:生成doc文档时对该项目的描述
- Package:包名称
- Version:版本信息,一个项目有好几个版本,SNAPSHOT表示快照,代表不稳定、尚处于开发中的版本;RELEASE版本则代表稳定的版本
3、点击next后,在 Spring boot Version 中选择一个稳定的版本,然后点 Next
4、填写项目名,这里的项目名我习惯性把它当作一个文件夹名,因为打包是不会根据这个名称来的,为了不混淆,我一般会跟之前的名称统一
5、点击Finish后,就会加载出这个项目的包结构,然后IDE的右下角会出现进度栏,因为需要下载 Spring 需要的包,根据经验,第一次下载会要很久。打开项目的 pom.xml 文件,里面红色的就是还在下的,下载完的就是白色的。
6、接着在dependencies标签中加入tomcat的依赖包
<!-- Spring Boot web包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
这是我的pom.xml里面的代码,仅供参考。关于标签: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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.xt</groupId>
<artifactId>core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-study</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot web包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
7、认识一下目录结构
- src/main/java/:这里保存整个程序的源代码,包括但不限于:controller、service、model
- src/main/resources/:这里保存整个程序的配置文件,包括但不限于:bootstrap.yml、application.yml
- src/test/:这里保存单元测试的代码
- ./pom.xml:项目中依赖以及插件都配置在这里,包括但不限于:spring依赖、mysql依赖
- .mvn、mvnw、mvnw.cmd这三个打包用的,一般在外面打包,所以可以将它们三兄弟删掉[非强制]
有两种比较流行的配置文件格式,第一种是 .properties 结尾的,第二种是 .yml 结尾的(YAML)
yml 是一种天然的树状结构,来看下区别
#application.yml
server:
port: 7001
#application.properties
server.port=7001
8、SpringBootStudyApplication类就是整个程序的启动类,程序启动会加载application.properties里面的配置,先看下启动类的代码
- @SpringBootApplication:表示这是一个spring boot应用
- main方法里面就是启动程序的代码,包括调用内置tomcat、加载配置文件等(但这里不深究)
9、找到启动类后,我们还需要简单的配置才能启动;一般应用中都会区分很多套环境,不同的环境都只改不同的配置,在这里我以两个环境为例。
10、在resources目录下删除 application.properties,新建三个文件,分别为:application.yml、application-local.yml、application-prod.yml,这里local后缀的为本地环境,prod(product)的为生产环境,而application则在应用启动的时候就会优先加载,然后我会在里面配置当前启动的环境。
特别注意:使用yml格式,在每个冒号后面都要有个空格,不加会导致报错或识别不到配置,被坑!
#配置应用程序绑定的端口
server:
port: 7001
spring:
application:
#指定应用程序名称
name: SpringBootStudy
profiles:
#指定启动的环境,格式:application- 后面的字符
active: local
11、只需要在 application.yml 中配置上面的信息,application-local 和 application-prod 不需要配置任何东西,就可以启动当前应用,启动时可以在启动日志中看到当前启动的环境(默认为:default)
12、能启动且不报错表示你的配置基本是没有问题的,接下来写一个MyController类。
package cn.xt.app.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
// @RestController 以JSON格式返回数据
@RestController
public class MyController {
// 请求名为:sayHello
// 请求方式为:get
@RequestMapping(value = "sayHello", method = RequestMethod.GET)
public String sayHello() {
return "hello world";
}
}
13、重新启动服务器后,我们输入:http://localhost:7001/sayHello,然后就是见证你成果的时候到了!
博客中项目Demo可进群免费下载:622038189