• 博客园Logo
  • 首页
  • 新闻
  • 博问
  • 专区
  • 闪存
  • 班级
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 简洁模式 ... 退出登录
    注册 登录
guangbinzhang
博客园    首页    新随笔    联系   管理    订阅  订阅

Spring Boot 整合Swagger2

一、 Swagger2有什么用?

作为一名程序猿写文档应该是件很痛苦的事,你有没有试过使用一个新框架的时候,照着官方写的文档可能根本搭建不起来服务? 有痛点好了,丝袜哥“swagger2”出现解决了这一问题。丝袜哥是一套restful风格的规范,只需要按照它的规范去定义接口及接口相关的信息。通过第三方工具定义好的注解(如:@Api,@ApiOperation),就可以做到生成各种格式的接口文档,以及在线接口调试页面等等。好了不多说了,说说遇到的问题?

二、开始整合

1、我使用的是第三方最新版本的3.0版
<!--3.0版本只需引用一个依赖-->
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-boot-starter</artifactId>
  <version>3.0.0</version>
</dependency>
2、创建SwaggerConfig.java
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.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
​
/**
* swagger配置文件
* zgb
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
  @Bean
  public Docket createRestApi() {
      return new Docket(DocumentationType.SWAGGER_2)
              .pathMapping("/")
              .select()
              .apis(RequestHandlerSelectors.basePackage("com.zgb.bzplat.controller"))
              .paths(PathSelectors.any())
              .build().apiInfo(new ApiInfoBuilder()
                      .title("zgb整合原生项目")
                      .description("zgb整合springboot相关项目")
                      .version("9.0")
                      .contact(new Contact("zhangguangbin", "361336231@qq.com", "361336231@qq.com"))
                      .license("")
                      .licenseUrl("361336231@qq.com")
                      .build());
​
  }
}
3、访问http://localhost:8080/swagger-ui/index.html.一定要注意url的变化、
2.9.X使用的路径为http://localhost:8080/swagger-ui.html .

二、关于使用第三方2.9.x版的问题

1、引用依赖不同
      <dependency>
          <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
          <version>2.9.6</version>
      </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
          <artifactId>springfox-swagger-ui</artifactId>
        <version>3.0.0</version>
    </dependency>
2、引用成功,后页面报404找不到只需要继承WebMvcConfigurationSupport ,编写以下代码将静态资源写入即可访问
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
 
  registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
  registry.addResourceHandler("/swaggerui.html").addResourceLocations("classpath:/META-INF/resources/");
  registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
​
}

 

posted @ 2020-09-10 02:49  zhangguangbin  阅读(283)  评论(0)  编辑  收藏  举报
刷新评论刷新页面返回顶部
Copyright © 2022 zhangguangbin
Powered by .NET 6 on Kubernetes