微服务项目整合SwaggerUI3.0接口文档分组和Header头定义

DBC 1.6K 0
温馨提示

看不懂你就直接粘贴!

package net.xdclass.config;

import lombok.Data;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.*;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;


@Component
@Data
@EnableOpenApi
public class SwaggerConfiguration {


    /**
     * 对C端用户的接口文档
     *
     * @return
     */
    @Bean
    public Docket webApiDoc() {

        return new Docket(DocumentationType.OAS_30)
                .groupName("用户端接口文档")
                .pathMapping("/")

                //定义是否开启Swagger,false是关闭,可以通过变量去控制,线上关闭
                .enable(true)

                //配置文档的元信息
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("net.xdclass"))
                //正则匹配请求路径,并分配到当前项目组
                .paths(PathSelectors.ant("/api/**"))
                .build()
                // 新版SwaggerUI3.0
                .globalRequestParameters(globalReqeustParameters())
                .globalResponses(HttpMethod.GET,getGlabalResponseMessage())
                .globalResponses(HttpMethod.POST,getGlabalResponseMessage());


    }


    /**
     * 对管理端的接口文档
     *
     * @return
     */
    @Bean
    public Docket adminApiDoc() {

        return new Docket(DocumentationType.OAS_30)
                .groupName("管理端接口文档")
                .pathMapping("/")

                //定义是否开启Swagger,false是关闭,可以通过变量去控制,线上关闭
                .enable(true)

                //配置文档的元信息
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("net.xdclass"))
                //正则匹配请求路径,并分配到当前项目组
                .paths(PathSelectors.ant("/admin/**"))
                .build();
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("1024电商平台")
                .description("微服务接口文档")
                .contact(new Contact("DBC", "QQ:957955071", "957955071@qq.com"))
                .version("v1.0")
                .build();
    }


    /**
     * 配置全局通用参数
     *
     * @return
     */
    private List<RequestParameter> globalReqeustParameters() {

        List<RequestParameter> parameters = new ArrayList<>();
        parameters.add(new RequestParameterBuilder()
                //令牌名字
                .name("token")
                .description("登录令牌")
                .in(ParameterType.HEADER)
                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
                //是否是必须的
                .required(false)
                .build());





//想添加多少个就添加多少个,因为项目可能会有多个参数
//        parameters.add(new RequestParameterBuilder()
//                .name("token2")
//                .description("登录令牌")
//                .in(ParameterType.HEADER)
//                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
//                .required(false)
//                .build());

        return parameters;

    }


    /**
     * 生成通用的响应信息
     */

    private List<Response> getGlabalResponseMessage() {

        List<Response> list = new ArrayList<>();
        list.add(new ResponseBuilder()
                .code("4xx")
                .description("请求错误,根据code和msg检查")
                .build());

        return list;
    }



}

发表评论 取消回复
表情 图片 链接 代码

分享