Swagger2 接口多级分组方法

swagger 无疑是Java开发的最佳伴侣,接口非常方便调试;当然也有用Postman,因人而异吧。
但是我们在配置Swagger的时候会有一个都默认一级分类也就是那个Defaul,在这个组里所有的接口二级分类都在这里,一般小项目还可以使用,但是做大项目的时候就接个接口就不那么方便了。
在这里插入图片描述

在这里人放一下我的Swagger 配置代码

@Bean
    public Docket buildDocket() {
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//调用下面apiInfo()方法
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.cm.aps"))//注意这里的路径,新手容易在这里出错导致打不开。
                .paths(PathSelectors.any())
                .build();
    }
    public ApiInfo apiInfo() {
        return  new ApiInfoBuilder()
                .title("模具排程相关 API")
                .description("部分数据来源:模德宝、快表数据")
                .termsOfServiceUrl("")//这里可以是项目地址
                .version("2.0.1")
                .build();
    }

    public ApiInfo apiInfoprdt() {
        return  new ApiInfoBuilder()
                .title("模具与产品 API")
                .description("基础数据配置")
                .termsOfServiceUrl("")//这里可以是项目地址
                .version("2.0.1")
                .build();
    }

那么我的目的是想把对应二级分组归到一组分类中,这样管理起来比较方便;
思路通过接口路径进行识别分组;
Swagger配置如下:

package com.cm.aps.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket buildDocket() {
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//调用下面apiInfo()方法
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.cm.aps"))//注意这里的路径,新手容易在这里出错导致打不开。
                .paths(PathSelectors.any())
                .build();
    }
    public ApiInfo apiInfo() {
        return  new ApiInfoBuilder()
                .title("模具排程相关 API")
                .description("部分数据来源:模德宝、快表数据")
                .termsOfServiceUrl("")//这里可以是项目地址
                .version("2.0.1")
                .build();
    }

    public ApiInfo apiInfoprdt() {
        return  new ApiInfoBuilder()
                .title("模具与产品 API")
                .description("基础数据配置")
                .termsOfServiceUrl("")//这里可以是项目地址
                .version("2.0.1")
                .build();
    }

    @Bean
    public Docket web_api_prdt() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfoprdt())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/prdt/**"))
                .build()
                .groupName("产品管理")
                .pathMapping("/");
    }

    @Bean
    public Docket web_api_setaps() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/setaps/**"))
                .build()
                .groupName("基础制程配置")
                .pathMapping("/");
    }

    //
}

接下来把对应的接口归类如下:
在这里插入图片描述
接下来看效果
在这里插入图片描述
可以写apiInfoprdt 方法进行API描述。

文章技术含量不大,只是一个小技巧,原创不易,欢迎评论,转载请注明出处。

  • 9
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Swagger2可以通过设置SecurityScheme来设置authorize。具体步骤如下: 1. 定义SecurityScheme 在Swagger2的配置类中,我们需要定义一个SecurityScheme,用于设置authorize。可以使用ApiKey、BasicAuth、OAuth2等多种认证方式,这里以OAuth2为例: ```java @Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .securitySchemes(Arrays.asList(securityScheme())) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } private SecurityScheme securityScheme() { GrantType grantType = new AuthorizationCodeGrantBuilder() .tokenEndpoint(new TokenEndpoint("http://localhost:8080/oauth/token", "oauthtoken")) .tokenRequestEndpoint( new TokenRequestEndpoint("http://localhost:8080/oauth/authorize", "client_id", "client_secret")) .build(); return new OAuthBuilder().name("oauth2") .grantTypes(Arrays.asList(grantType)) .scopes(Arrays.asList(scopes())) .build(); } private AuthorizationScope[] scopes() { AuthorizationScope[] scopes = { new AuthorizationScope("read", "for read operations"), new AuthorizationScope("write", "for write operations") }; return scopes; } } ``` 2. 设置SecurityRequirement 在接口文档中,需要通过SecurityRequirement来指定哪些接口需要认证,哪些不需要认证。可以设置全局的SecurityRequirement,也可以为每个接口单独设置SecurityRequirement。这里以全局设置为例: ```java @Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .securitySchemes(Arrays.asList(securityScheme())) .securityContexts(Arrays.asList(securityContext())) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } private SecurityScheme securityScheme() { //... } private SecurityContext securityContext() { return SecurityContext.builder() .securityReferences(Arrays.asList(new SecurityReference("oauth2", scopes()))) .forPaths(PathSelectors.any()) .build(); } private AuthorizationScope[] scopes() { //... } } ``` 这样,就可以在接口文档中看到Authorize按钮了。点击按钮后,会弹出一个对话框,要求输入OAuth2的认证信息,然后就可以访问需要认证的接口了。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值