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

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-06-30 11:21:13

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.cn
  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. }
  36. 3.Swagger 拦截配置


  37. package org.fh.config;


  38. import org.springframework.context.annotation.Configuration;
  39. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  40. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  41. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


  42. /**
  43.  * 说明:Swagger 拦截配置
  44.  * 作者:FH Admin
  45.  * from fhadmin.cn
  46.  */
  47. @Configuration
  48. public class WebMvcConfig implements WebMvcConfigurer {


  49.     @Override
  50.     public void addResourceHandlers(ResourceHandlerRegistry registry) {
  51.         registry.
  52.                 addResourceHandler("/swagger-ui/**")
  53.                 .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
  54.                 .resourceChain(false);
  55.     }


  56.     @Override
  57.     public void addViewControllers(ViewControllerRegistry registry) {
  58.         registry.addViewController("/swagger-ui/")
  59.                 .setViewName("forward:/swagger-ui/index.html");
  60.     }
  61. }

4. 访问 127.0.0.1:8081/swagger-ui/index.html
5. 接口说明案例

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

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

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