阳光沙滩-课程笔记 阳光沙滩-课程笔记
首页 (opens new window)
VIP (opens new window)
  • 课程笔记

    • 《Android项目喜马拉雅FM》
    • 《Android项目领券联盟》
    • 《AndroidStudio技巧》
    • 《Android自定义控件》
    • 《Android开发基础》
    • 《Android约束布局》
    • 《AOSP安卓开源项目》
    • 《RecyclerView》
  • 《领券联盟Nuxt.js》
  • 《博客系统后台管理系统vue.js》
  • 《博客系统门户Nuxt.js》
  • 《博客系统前后端分离后台》
  • 《博客系统部署》
  • 《摸鱼君后台》
  • 《OTA升级管理系统》
  • 阳光沙滩API (opens new window)
  • 领券联盟API (opens new window)
  • 博客系统API (opens new window)
首页 (opens new window)
VIP (opens new window)
  • 课程笔记

    • 《Android项目喜马拉雅FM》
    • 《Android项目领券联盟》
    • 《AndroidStudio技巧》
    • 《Android自定义控件》
    • 《Android开发基础》
    • 《Android约束布局》
    • 《AOSP安卓开源项目》
    • 《RecyclerView》
  • 《领券联盟Nuxt.js》
  • 《博客系统后台管理系统vue.js》
  • 《博客系统门户Nuxt.js》
  • 《博客系统前后端分离后台》
  • 《博客系统部署》
  • 《摸鱼君后台》
  • 《OTA升级管理系统》
  • 阳光沙滩API (opens new window)
  • 领券联盟API (opens new window)
  • 博客系统API (opens new window)
  • 单体应用博客系统

  • 博客系统部署

  • 摸鱼君微服务项目实战

    • 摸鱼君介绍
    • 数据库创建&表字段设计
    • Mybatis-plus
    • 项目创建
    • 编写u-center模块功能
    • 管理中心项目准备
    • 编写mo-yu-jun模块功能
    • 微服务结构
    • 服务注册中心
    • 网关
    • API文档
    • OTA升级管理系统

    • 后台
    • 摸鱼君微服务项目实战
    TrillGates
    2022-03-28
    目录

    API文档

    # API文档

    为了方便前端的开发人员,我们自动生成一个API文档,方便前端开发和测试。

    通常我们使用Swagger2,但是UI简单,也可以结合第三方的UI-knife4j

    https://doc.xiaominfo.com/
    
    1

    # gateway依赖

    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>knife4j-spring-boot-starter</artifactId>
        <version>3.0.3</version>
    </dependency>
    
    1
    2
    3
    4
    5

    文档入口

    @RestController
    public class SwaggerApi {
    
        @Autowired(required = false)
        private SecurityConfiguration securityConfiguration;
    
        @Autowired(required = false)
        private UiConfiguration uiConfiguration;
    
        private final SwaggerResourcesProvider swaggerResources;
    
        @Autowired
        public SwaggerApi(SwaggerResourcesProvider swaggerResources) {
            this.swaggerResources = swaggerResources;
        }
    
    
        @GetMapping("/swagger-resources/configuration/security")
        public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
            return Mono.just(new ResponseEntity<>(
                    Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
        }
    
        @GetMapping("/swagger-resources/configuration/ui")
        public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
            return Mono.just(new ResponseEntity<>(
                    Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
        }
    
        @GetMapping("/swagger-resources")
        public Mono<ResponseEntity> swaggerResources() {
            return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34

    资源处理器

    @Slf4j
    @Component
    @Primary
    @AllArgsConstructor
    public class SwaggerResourceConfig implements SwaggerResourcesProvider {
    
        private final RouteLocator routeLocator;
        private final GatewayProperties gatewayProperties;
    
    
        @Override
        public List<SwaggerResource> get() {
            List<SwaggerResource> resources = new ArrayList<>();
            List<String> routes = new ArrayList<>();
            routeLocator.getRoutes().subscribe(route -> routes.add(route.getId()));
            gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId())).forEach(route -> {
                route.getPredicates().stream()
                        .filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName()))
                        .forEach(predicateDefinition -> resources.add(swaggerResource(route.getId(),
                                predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0")
                                        .replace("**", "v2/api-docs"))));
            });
    
            return resources;
        }
    
        private SwaggerResource swaggerResource(String name, String location) {
            log.info("name:{},location:{}",name,location);
            SwaggerResource swaggerResource = new SwaggerResource();
            swaggerResource.setName(name);
            swaggerResource.setLocation(location);
            swaggerResource.setSwaggerVersion("2.0");
            return swaggerResource;
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35

    # 其他模块依赖:

    <!--swagger ui-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
    
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

    配置:

    knife4j:
      # 开启增强配置
      enable: true
      # 是否生产环境 默认否;生产环境配置为true,将无法访问文档
      production: false
      basic:
        enable: false
    
    1
    2
    3
    4
    5
    6
    7

    模块配置

    @Configuration
    @EnableSwagger2
    public class SwaggerConfiguration {
    
        @Bean(value = "UCenter")
        @Order(value = 1)
        public Docket groupRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(groupApiInfo())
                    .select().apis(RequestHandlerSelectors.basePackage("net.sunofbeach.uc.api"))
                    .paths(PathSelectors.any()
                    ).build();
        }
    
        private ApiInfo groupApiInfo() {
            return new ApiInfoBuilder()
                    .title("阳光沙滩-摸鱼君")
                    .description("统一用户中心模块")
                    .termsOfServiceUrl("localhost")
                    .licenseUrl("www.sunofbeach.net")
                    .contact(new Contact("拉大锯", "", "sunofbeach@gmail.com"))
                    .version("1.0").build();
        }
    
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25

    以ucenter为例,一个模块开启和配置Swager2

    访问路径:

    ip:端口号/swagger.html
    locahost:40100/swagger-ui.html
    
    1
    2
    编辑 (opens new window)
    网关
    OTA升级管理系统介绍

    ← 网关 OTA升级管理系统介绍→

    Theme by Vdoing | Copyright © 2022-2022 sunofbeach.net
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式