• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

springboot2.x集成swagger的方法示例

springboot 搞代码 4年前 (2022-01-09) 24次浏览 已收录 0个评论

集成swagger

pom包配置

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.9.2</version>
</dependency>
<!-- swagger-ui -->
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>${swagger.version}&l<strong>本文来源gaodai#ma#com搞@代~码^网+</strong>t;/version>
</dependency>

添加Swagger配置文件

@Configuration
@EnableSwagger2
public class SwaggerConfig {
  /**
   * 创建一个Docket对象
   * 调用select()方法,
   * 生成ApiSelectorBuilder对象实例,该对象负责定义外漏的API入口
   * 通过使用RequestHandlerSelectors和PathSelectors来提供Predicate,在此我们使用any()方法,将所有API都通过Swagger进行文档管理
   * @return
   */
  @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.any())
        .paths(PathSelectors.any())
        .build();
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        //标题
        .title("Spring Boot中使用Swagger2构建RESTful APIs")
        //简介
        .description("")
        //服务条款
        .termsOfServiceUrl("")
        //作者个人信息
        .contact(new Contact("chenguoyu","","[email protected]"))
        //版本
        .version("1.0")
        .build();
  }
}

如果不想将所有的接口都通过swagger管理的话,可以将RequestHandlerSelectors.any()修改为RequestHandlerSelectors.basePackage()

配置静态访问资源

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    // 解决 swagger-ui.html 404报错
    registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
  }
}

到这里为止swagger就已经配置完了,可以启动项目,然后访问如下链接即可http://localhost:9000/swagger…

端口号applicationContext中设置的端口号。

集成swagger-bootstrap-ui

由于个人感觉原生的swagger-ui不太好看,网上提供了swagger-bootstrap-ui。

pom依赖

<dependency>
  <groupId>com.github.xiaoymin</groupId>
  <artifactId>swagger-bootstrap-ui</artifactId>
  <version>1.9.3</version>
</dependency>

配置静态访问资源

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    // 解决 swagger-ui.html 404报错
    registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
    // 解决 doc.html 404 报错
    registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");

  }
}

这时只需要访问以下链接即可http://localhost:9000/doc.html

swagger常用注解

@Api:用在类上,标志此类是Swagger资源

属性名称 备注
value 该参数没什么意义,在UI界面上不显示,所以不用配置
tags 说明该类的作用,参数是个数组,可以填多个
description 对api资源的描述

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:springboot2.x集成swagger的方法示例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址