配置swagger2生成API接口

一、Swagger2介绍
前后端分离开发模式中,api文档是最好的沟通方式。
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服
务。
1. 及时性 (接口变更后,能够及时准确地通知相关前后端开发人员)
2. 规范性 (并且保证接口的规范性,如接口的地址,请求方式,参数及响应格式和错误信息)
3. 一致性 (接口信息一致,不会出现因开发人员拿到的文档版本不一致,而出现分歧)
4. 可测性 (直接在接口文档上进行测试,以方便理解业务)
二、配置Swagger2
1 、创建 common 模块
yygh_parent 下创建模块 common
配置:
groupId com.atguigu
arƟ factId common

2、在common中引入相关依赖

1 <dependencies>
2 <dependency>
3 <groupId> org.springframework.boot </groupId>
4 <artifactId> spring-boot-starter-web </artifactId>
5 <scope> provided </scope>
6 </dependency>
7
8 <!--mybatis-plus-->
9 <dependency>
10 <groupId> com.baomidou </groupId>
11 <artifactId> mybatis-plus-boot-starter </artifactId>
12 <scope> provided </scope>
13 </dependency>
14
15 <!--lombok 用来简化实体类:需要安装 lombok 插件 -->
16 <dependency>
17 <groupId> org.projectlombok </groupId>
18 <artifactId> lombok </artifactId>
19 </dependency>
20
21 <!--swagger-->
22 <dependency>
23 <groupId> io.springfox </groupId>
24 <artifactId> springfox-swagger2 </artifactId>
25 </dependency>
26 <dependency>
27 <groupId> io.springfox </groupId>
28 <artifactId> springfox-swagger-ui </artifactId>
29 </dependency>
30
31 <dependency>
32 <groupId> com.alibaba </groupId>
33 <artifactId> fastjson </artifactId>
34 </dependency>
35 </dependencies>
3、在common下面创建子模块service_utils
3 、在模块 service-base 中,创建 swagger 的配置类
创建包 com.atguigu.yygh.common.config ,创建类 Swagger2Config

 

1 @Configuration
2 @EnableSwagger2
3 public class Swagger2Config {
4 @Bean
5 public Docket webApiConfig(){
6 return new Docket(DocumentationType.SWAGGER_2)
7 .groupName( "webApi" )
8 .apiInfo(webApiInfo())
9 .select()
10 // 只显示 api 路径下的页面
11 //.paths(Predicates.and(PathSelectors.regex("/api/.*")))
12 .build();
13 }
14
15 @Bean
16 public Docket adminApiConfig(){
17 return new Docket(DocumentationType.SWAGGER_2)
18 .groupName( "adminApi" )
19 .apiInfo(adminApiInfo())
20 .select()
21 // 只显示 admin 路径下的页面
22 .paths(Predicates.and(PathSelectors.regex( "/admin/.*" )))
23 .build();
24 }
25 26 private ApiInfo webApiInfo(){
27 return new ApiInfoBuilder()
28 .title( " 网站 -API 文档 " )
29 .description( " 本文档描述了网站微服务接口定义 " )
30 .version( "1.0" )
31 .contact( new Contact( "atguigu" , "http://atguigu.com" , "493211102@qq.c
32 .build();
33 }
34
35 private ApiInfo adminApiInfo(){
36 return new ApiInfoBuilder()
37 .title( " 后台管理系统 -API 文档 " )
38 .description( " 本文档描述了后台管理系统微服务接口定义 " )
39 .version( "1.0" )
40 .contact( new Contact( "atguigu" , "http://atguigu.com" , "49321112@qq.co
41 .build();
42 }
43 }
4 、在模块 service 模块中引入 service_utils
1 <dependency>
2 <groupId> com.atguigu </groupId>
3 <artifactId> service_utils </artifactId>
4 <version> 0.0.1-SNAPSHOT </version>
5 </dependency>
5 、在 service_hosp 启动类上添加注解,进行测试

6、通过地址访问测试

 

可以添加一些自定义设置,例如:
定义样例数据
1 @ApiModelProperty (value = " 创建时间 " , example = "2019-01-01 8:00:00" )
2 @TableField (fill = FieldFill.INSERT)
3 private Date gmtCreate;
4
5 @ApiModelProperty (value = " 更新时间 " , example = "2019-01-01 8:00:00" )
6 @TableField (fill = FieldFill.INSERT_UPDATE)
7 private Date gmtModified;
5、定义接口说明和参数说明
定义在方法上: @ApiOperation
定义在参数上: @ApiParam
1 // 医院设置接口
2 @Api (description = " 医院设置接口 " )
3 @RestController
4 @RequestMapping ( "/admin/hosp/hospitalSet" )
5 public class HospitalSetController {
6
7 @Autowired
8 private HospitalSetService hospitalSetService;
9
10 // 查询所有医院设置
11 @ApiOperation (value = " 医院设置列表 " )
12 @GetMapping ( "findAll" )
13 public List<HospitalSet> findAll() {
14 List<HospitalSet> list = hospitalSetService.list();
15 return list;
16 }
17 18 @ApiOperation (value = " 医院设置删除 " )
19 @DeleteMapping ( "{id}" )
20 public boolean removeById( @ApiParam (name = "id" , value = " 讲师 ID" , required = tru
21 return hospitalSetService.removeById(id);
22 }
23 }

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做一道光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值