创建SpringBoot项目
这里是用IDEA做演示,安装配置好开发工具,前面有讲我是这样使用SpringBoot(eclipse&IDEA配置JDK Maven docker)。这一章蒋完成SpringBoot项目的创建,开放一个简单的API接口。通过postman访问API接口。
创建根目录
打开IDEA,到创建项目界面。这里创建一个父模板,后面各个项目以子模块的方式进行。
点击Create New Project,选中Maven项目,不要选择Create From archetype,点击Next
输入组名(GroupId)、项目名(ArtifactId)、版本号(Version),后点击next
选择目录后点击Finish
弹出创建目录的对话框点击OK
进入IDEA项目界面。
这是parent项目,不需要src。因此,把src目录删除。
引入SpringBoot
在parent项目的pom.xml中包含子模块中都会用到的模块,如SpringBoot、lombok等。把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>com.biboheart.demos</groupId>
<artifactId>bhparent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- 我常用的一些工具写在这个包里,源码地址:https://gitee.com/biboheart/brick 已经在maven中央仓库发布 -->
<dependency>
<groupId>com.biboheart</groupId>
<artifactId>bh-brick</artifactId>
<version>0.0.6</version>
</dependency>
</dependencies>
</project>
当前SpringBoot稳定的最新版是2.1.3,我们就用2.1.3来演示吧。
spring boot 官网中选中Learn可以查看。点击Reference Doc.可以查看spring boot此版本的官方文档。
这个版本要求Java8及以上版本,Maven 3.3以上
这时,界面状态
创建第一个项目
先来一个最基本的项目,只是提供一个API。
-
在bhparent项目名上右键->New->Module
-
选择Maven项目,勾选Create from archetype,使用“maven-archetype-puickstart”,点击“Next”
-
填写模块ArtifactId为bhhello,GroupId与Version就用默认的,如果有改变,在模块的pom.xml文件也可以修改。
-
确认信息,Maven的配置项在前面已经讲过。这里可以修改配置文件。
-
模块名称。前面(第2步)填写的是模块ID,是maven项目属性,是全局的,如果发布到中央仓库,可以用来拉取模块。这里填的是模块名称,也就是项目目录,是本地的。IDEA默认为模块ArtifactId,可以修改。这里就不改了。
-
点击Finish完成创建。在IDEA右下方查看创建进度。
项目结构
一个基本的maven项目的目录结构,是这样的。在项目根目录一个pom.xml文件和src文件夹,src文件夹中存放项目源码。在src目录下是main和test两个文件夹,main文件夹下存放主要的项目文件,test文件下存放测试的文件。main和test下的目录结构一样,包含一个java目录和resources目录。java目录中存放java源代码,resources目录中存放配置文件,资源文件等。一般情况下,test中测试main中的类,使用相同的包名。如图,com.biboheart.demos。
IDEA默认生成的项目文件名为App和AppTest。这个项目不做测试,删除test/jave/下的包和文件。main中的App改名成HelloApplication。
IDEA的更名操作,右键App文件,Refactor->Rename
完成后项目结构
修改bhhello项目的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">
<parent>
<artifactId>bhparent</artifactId>
<groupId>com.biboheart.demos</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>bhhello</artifactId>
<name>bhhello</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
</dependencies>
</project>
项目开发
修改HelloApplication文件内容如下
package com.biboheart.demos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class HelloApplication {
public static void main( String[] args ) {
SpringApplication.run(HelloApplication.class, args);
}
@RequestMapping(value = "/hello")
public String hello(String name) {
return "hello " + name;
}
}
在IDEA中运行项目
HelloApplication上右键点击,选择Debug 'HelloApplication',调试运行项目
项目开始启动,在控制台中会打印出运行信息。
在调试窗口中看到Undertow服务器已经启动,端口号为8080。项目已经完成启动。点击左边的红色方框可以停止。
测试
浏览器中GET测试。
打开postman,或其它http调试工具。如何安装postman的问题请通过其它途径获取。
用GET方式请求。
用POST方式请求
代码分析
这个项目已经完成,到目前为止,这个项目创建,目录调整,maven依赖。进行了一个HelloApplication.java文件的编写。只是做了少量的开发,就完成了一个API服务。这就是spring boot的强大之处,快速构建项目。
下面分析下代码
package com.biboheart.demos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class HelloApplication {
public static void main( String[] args ) {
SpringApplication.run(HelloApplication.class, args);
}
@RequestMapping(value = "/hello")
public String hello(String name) {
return "hello " + name;
}
}
- @SpringBootApplication
表示这是一个spring boot项目的入口。它包含了一些自动配置的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
- @RestController
表示这是一个RESTful风格的API。配合class中的函数,实现API开放。 - main函数
public static void main( String[] args ) {
SpringApplication.run(HelloApplication.class, args);
}
使用org.springframework.boot.SpringApplication类的run函数指定项目入口类,启动项目。
- @RequestMapping(value = "/hello")
表示开放一个API,URI为“/hello”,API的实现是注解下面的函数。 - public String hello(String name) {}
实现“/hello”,可以接收名为name的字符串参数。返回结果为String类型的字符串。当客户访问“http://localhost:8080/hello”时,会找到这个函数执行,并返回执行结果。这里是RESTful风格返回。 - spring boot默认服务端口是8080,可以配置,这个后面章节会讲到。