Github:https://github.com/SpringForAll/spring-boot-starter-swagger
一、pom文件
<!-- swagger2 -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.0.RELEASE</version>
</dependency>
二、增加Swagger2注解
@EnableSwagger2Doc
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
三、修改Springboot配置文件
swagger:
title: 标题
version: 版本
base-package: swagger扫描的基础包,默认:全扫描
enabled: 是否启用swagger,默认:true
# 显示请求头
ui-config.show-request-headers: true
# 取消使用默认预定义的响应消息,并使用自定义响应消息
apply-default-response-messages: false
global-response-message.post[0]:
code: 200
message: OK
global-response-message.post[1]:
code: 201
message: 保存成功
global-response-message.post[2]:
code: 400
message: 错误请求
global-response-message.post[3]:
code: 401
message: 令牌错误
global-response-message.post[4]:
code: 403
message: 禁止请求
global-response-message.post[5]:
code: 404
message: 未找到文件
global-response-message.post[6]:
code: 413
message: 请求的实体过大
global-response-message.post[7]:
code: 1000
message: 保存失败
global-response-message.post[8]:
code: 500
message: 未知错误
四、测试
1、启动项目,并访问http://IP/swagger-ui.html