简述
本文中,将使用spring boot
来构建服务,使用swagger2来提供api ui。最终部署到linux下tomcat中。
源代码
环境
IDE: IntelliJ IDEA
JDK: 1.8.0_u162
Framework: Spring Boot, Swagger2
WebServer: Tomcat(本地端口8088)
开发
工程结构
1 | --root(maven pom父工程) |
root
pom.xml中值得注意的只有packaging节点,类型为pom
1
<packaging>pom</packaging>
iarc-entity
设置root作为parent,使用lombok来简化Java代码1
2
3
4
5
6
7
8
9
10
11
12
13<parent>
<groupId>cn.icmyfuture.iarc</groupId>
<artifactId>root</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
iarc-service
pom.xml
中packaging
节点类型为war
,dependencies/build使用spring boot标准配置1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27<packaging>war</packaging>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
...
<!-- springfox -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger2.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger2.version}</version>
</dependency>
...
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>端口和JMX唯一性配置
./iarc-service/src/main/resources/application.properties
中设置参数1
2
3server.port=8001
endpoints.jmx.unique-names=true
spring.jmx.default-domain=iarc.dataswagger2 configuration class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36package cn.icmyfuture.iarc.service.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.icmyfuture.iarc.service.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot using Swagger2 for RESTful APIs")
.description("ivan's swagger2")
.termsOfServiceUrl("http://localhost:8001")
.contact("ivan")
.version("1.0")
.build();
}
}
调试
IntelliJ IDEA中Run ServiceApplication
,使用spring boot内置的tomcat容器作为宿主,
- 输入
http://localhost:8001/user/getUser/123
来进行验证 - 输入
http://localhost:8001/swagger-ui.html
来查看api ui
部署
打包
打开IntelliJ IDEA中Maven Projects
窗口。由于使用了maven父工程,打包时需要先执行root相关操作
root
1
Lifecycle - 依次clean compile install
iarc-service
1
Lifecyle - 依次clean compile package
部署到linux下tomcat中
- linux下jdk以及tomcat的安装/自启动可参考这里
- 将打包好的iarc-service.war复制到
$CATALINA_HOME/webapps
目录下,tomcat会自动解压war包,访问http://localhost:8080/iarc-service/user/getUser/123
进行验证