SpringBoot中整合Swagger2并且添加消息头

相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还是为了以后交接方便,都有要求写API文档。

手写Api文档的几个痛点:

  1. 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时。
  2. 接口返回结果不明确
  3. 不能直接在线测试接口,通常需要使用工具,比如postman
  4. 接口文档太多,不好管理

Swagger也就是为了解决这个问题,当然也不能说Swagger就一定是完美的,当然也有缺点,最明显的就是代码移入性比较强。

其他的不多说,想要了解Swagger的,可以去Swagger官网,可以直接使用Swagger editor编写接口文档,当然我们这里讲解的是SpringBoot整合Swagger2,直接生成接口文档的方式。

一、添加依赖

  1. <!-- Swagger相关依赖包  -->
  2.         <dependency>
  3.             <groupId>io.springfox</groupId>
  4.             <artifactId>springfox-swagger2</artifactId>
  5.             <version>2.2.2</version>
  6.         </dependency>
  7.         <dependency>
  8.             <groupId>io.springfox</groupId>
  9.             <artifactId>springfox-swagger-ui</artifactId>
  10.             <version>2.2.2</version>
  11.         </dependency>

二、Swagger配置类

  1. import java.util.Collections;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import springfox.documentation.builders.ApiInfoBuilder;
  5. import springfox.documentation.builders.ParameterBuilder;
  6. import springfox.documentation.builders.PathSelectors;
  7. import springfox.documentation.builders.RequestHandlerSelectors;
  8. import springfox.documentation.schema.ModelRef;
  9. import springfox.documentation.service.ApiInfo;
  10. import springfox.documentation.spi.DocumentationType;
  11. import springfox.documentation.spring.web.plugins.Docket;
  12. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  13. @Configuration
  14. @EnableSwagger2
  15. public class Swagger2 {
  16.     @Bean
  17.     public Docket createRestApi() {
  18.         return new Docket(DocumentationType.SWAGGER_2)
  19.                 .apiInfo(apiInfo())
  20.                 .select()
  21.                 .apis(RequestHandlerSelectors.basePackage("包名称"))
  22.                 .paths(PathSelectors.any())
  23.                 .build()
  24.                 .globalOperationParameters(
  25.                         Collections.singletonList(new ParameterBuilder()
  26.                         .name("token")
  27.                         .description("token")
  28.                         .modelRef(new ModelRef("string"))
  29.                         .parameterType("header")
  30.                         .required(false)
  31.                         .build())
  32.                 );
  33.     }
  34.     private ApiInfo apiInfo() {
  35.         return new ApiInfoBuilder()
  36.                 .title("标题名称")
  37.                 .description("描述")
  38.                 .termsOfServiceUrl("服务网址")
  39.                 .contact("作者")
  40.                 .version("版本")
  41.                 .build();
  42.     }
  43. }
  44.  

注:用@Configuration注解该类,等价于XML中配置beans;用@Bean标注方法等价于XML中配置bean。

      @EnableSwagger2表示开启Swagger

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值