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

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-12-13 12:53:33


点击(此处)折叠或打开


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

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

  7. 2. 配置类SwaggerConfig

  8. package org.fh.config;

  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;

  11. import springfox.documentation.builders.ApiInfoBuilder;
  12. import springfox.documentation.builders.PathSelectors;
  13. import springfox.documentation.builders.RequestHandlerSelectors;
  14. import springfox.documentation.oas.annotations.EnableOpenApi;
  15. import springfox.documentation.service.ApiInfo;
  16. import springfox.documentation.spi.DocumentationType;
  17. import springfox.documentation.spring.web.plugins.Docket;

  18. /**
  19.  * 说明:Swagger 接口API生成
  20.  * 作者:FH Admin
  21.  * from fhadmin.org
  22.  */
  23. @Configuration
  24. @EnableOpenApi
  25. public class SwaggerConfig {

  26.     @Bean
  27.     public Docket createRestApi() {
  28.         return new Docket(DocumentationType.OAS_30)
  29.                 .apiInfo(apiInfo())
  30.                 .select()
  31.                 .apis(RequestHandlerSelectors.basePackage("org.fh.controller"))    // 为当前包路径
  32.                 .paths(PathSelectors.any())
  33.                 .build();
  34.     }

  35.     private ApiInfo apiInfo() {
  36.         return new ApiInfoBuilder()
  37.                 .title("FH Admin Swagger3 RESTful API")     // 页面标题
  38.                 .version("3.0")                                // 版本号
  39.                 .description("fhadmin.org")                 // 描述
  40.                 .build();
  41.     }

  42. }
  43. 3.Swagger 拦截配置

  44. package org.fh.config;

  45. import org.springframework.context.annotation.Configuration;
  46. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  47. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  48. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

  49. /**
  50.  * 说明:Swagger 拦截配置
  51.  * 作者:FH Admin
  52.  * from fhadmin.org
  53.  */
  54. @Configuration
  55. public class WebMvcConfig implements WebMvcConfigurer {

  56.     @Override
  57.     public void addResourceHandlers(ResourceHandlerRegistry registry) {
  58.         registry.
  59.                 addResourceHandler("/swagger-ui/**")
  60.                 .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
  61.                 .resourceChain(false);
  62.     }

  63.     @Override
  64.     public void addViewControllers(ViewControllerRegistry registry) {
  65.         registry.addViewController("/swagger-ui/")
  66.                 .setViewName("forward:/swagger-ui/index.html");
  67.     }
  68. }

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

  70. 5.接口说明案例

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

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


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