Chinaunix首页 | 论坛 | 博客
  • 博客访问: 604257
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-08-30 13:27:24

 1.引入依赖,版本3.0.0只引入一个即可

点击(此处)折叠或打开

  1. <dependency>
  2.          <groupId>io.springfox</groupId>
  3.          <artifactId>springfox-boot-starter</artifactId>
  4.          <version>3.0.0</version>
  5.         </dependency>

2. 配置类SwaggerConfig

点击(此处)折叠或打开

  1. package org.fh.config;

  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.PathSelectors;
  6. import springfox.documentation.builders.RequestHandlerSelectors;
  7. import springfox.documentation.oas.annotations.EnableOpenApi;
  8. import springfox.documentation.service.ApiInfo;
  9. import springfox.documentation.spi.DocumentationType;
  10. import springfox.documentation.spring.web.plugins.Docket;

  11. /**
  12.  * 说明:Swagger 接口API生成
  13.  * 作者:FH Admin
  14.  * from fhadmin.org
  15.  */
  16. @Configuration
  17. @EnableOpenApi
  18. public class SwaggerConfig {

  19.     @Bean
  20.     public Docket createRestApi() {
  21.         return new Docket(DocumentationType.OAS_30)
  22.                 .apiInfo(apiInfo())
  23.                 .select()
  24.                 .apis(RequestHandlerSelectors.basePackage("org.fh.controller"))    // 为当前包路径
  25.                 .paths(PathSelectors.any())
  26.                 .build();
  27.     }

  28.     private ApiInfo apiInfo() {
  29.         return new ApiInfoBuilder()
  30.                 .title("FH Admin Swagger3 RESTful API")     // 页面标题
  31.                 .version("3.0")                                // 版本号
  32.                 .description("fhadmin.org")                 // 描述
  33.                 .build();
  34.     }

  35. }

3.Swagger 拦截配置

点击(此处)折叠或打开

  1. package org.fh.config;

  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  4. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  5. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

  6. /**
  7.  * 说明:Swagger 拦截配置
  8.  * 作者:FH Admin
  9.  * from fhadmin.org
  10.  */
  11. @Configuration
  12. public class WebMvcConfig implements WebMvcConfigurer {

  13.     @Override
  14.     public void addResourceHandlers(ResourceHandlerRegistry registry) {
  15.         registry.
  16.                 addResourceHandler("/swagger-ui/**")
  17.                 .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
  18.                 .resourceChain(false);
  19.     }

  20.     @Override
  21.     public void addViewControllers(ViewControllerRegistry registry) {
  22.         registry.addViewController("/swagger-ui/")
  23.                 .setViewName("forward:/swagger-ui/index.html");
  24.     }
  25. }

4.访问 127.0.0.1:8081/swagger-ui/index.html


5.接口说明案例

点击(此处)折叠或打开

  1. 处理类上加注解,比如
  2. @Api("用户注册登录接口")

  3. 在方法上加注解,比如
  4. @ApiOperation(value = "登录", notes="校验登录是否成功")
  5. @ApiImplicitParam(name = "KEYDATA", value = "用户名密码混淆码组合", paramType = "query", required = true, dataType = "String")


阅读(994) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~