首页 > 其他分享 >Swagger + knife4j ,并设置header入参

Swagger + knife4j ,并设置header入参

时间:2024-02-27 15:01:22浏览次数:20  
标签:knife4j Swagger parameters documentation header new import springfox builders

配置代码

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.builders.RequestParameterBuilder;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

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

/**
 * swagger配置
 *
 */
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
    
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.haier.hibp.plan.baseinfo"))
            .paths(PathSelectors.any())
            .build()
            .globalRequestParameters(getGlobalRequestParameters());
    }

    //生成全局通用参数
    private List<RequestParameter> getGlobalRequestParameters() {
        List<RequestParameter> parameters = new ArrayList<>();
        parameters.add(new RequestParameterBuilder().name("x-user-id").description("用户编号").in(ParameterType.HEADER)
            .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)).defaultValue("20110116"))
            .required(true)
            .build());
        return parameters;
    }
    
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("hibp-plan-baseinfo RESTful APIs")
            .build();
    }

}

参考

https://blog.csdn.net/ladymorgana/article/details/134648338

标签:knife4j,Swagger,parameters,documentation,header,new,import,springfox,builders
From: https://www.cnblogs.com/shareToAll/p/18036889

相关文章

  • Swagger2注释篇
    API使用说明作用范围APIAPI常用参数作用位置协议集描述@Api@Api(tags={"tag1","tag2","..."})controller类协议描述@ApiOperation@ApiOperation(value="功能描述",notes="备注")controller类的方法描述返回对象的意义@ApiModel@ApiModel......
  • Unity编辑器扩展秘籍-利用Editor.finishedDefaultHeaderGUI增加Header功能
    利用Editor.finishedDefaultHeaderGUI这个回调可以实现自定义Header菜单usingUnityEditor;usingUnityEngine;namespaceYaojz{[InitializeOnLoad]publicstaticclassDefaultHeaderDrawer{staticDefaultHeaderDrawer(){E......
  • 项目整合swagger
    1、添加依赖       <!--swagger--><dependency><groupId>com.spring4all</groupId><artifactId>swagger-spring-boot-starter</artifactId></dependency>为防止报错,请额外添加下面依赖:<d......
  • bits/libc-header-start.h: No such file or directory
    问题出现在编译一个工程的时候,出现了报错Infileincludedfrom/usr/lib/gcc/x86_64-linux-gnu/9/include/stdint.h:9,frommain.c:1:/usr/includ......
  • java +swagger3项目配置
    1.添加依赖<!--swagger3--><dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></depen......
  • Ubuntu编译报错:fatal error: bits/libc-header-start.h: No such file or directory
    Ubuntu编译报错:fatalerror:bits/libc-header-start.h:Nosuchfileordirectory具体报错如下:fromhost/minilua.c:33:/usr/include/limits.h:26:10:fatalerror:bits/libc-header-start.h:Nosuchfileordirectory26|#include<bits/libc-header-start.h>......
  • 用Pycharm把浏览器复制出来的headers/参数给字段和值分别加单引号
    要解决的问题:从浏览器复制出来的headers/参数样式User-Agent:Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/121.0.0.0Safari/537.36Referer: https://www.google.com.hk/ 想要在Python中能用,需要改成字典的样式,给每个......
  • [cpp]: thread -- with header <syncstream>
    [cpp]: thread--withheader<syncstream>    一、说明: 1、【并发编程】thread编程中的同步输出:std::osyncstream// Definedinheader <syncstream>   二、程序代码1#include<iostream>2#include<string>3#include<......
  • swagger的使用
    swagger的使用1、导入依赖<!--swagger依赖--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version>......
  • spring boot swagger knife4j使用
    Swagger2已经在17年停止维护了,取而代之的是Swagger3(基于openApi3),这篇文章将介绍如何在java中使用OpenApi3(swagger3)以及与swagger2的对比。1.基本介绍1.1OpenAPIOpenApi是业界真正的api文档标准,其是由Swagger来维护的,并被linux列为api标准,从而成为行业标准。1.2Swag......