1.在上上一遍  SpringCloud Zuul网关整合Swagger在网关swagger-ui.html查看各个服务的接口文档  https://blog.csdn.net/weixin_39494923/article/details/86287486 的基础上补充添加头部信息,

当我们需要传递获取Header头部携带消息时,也需要在Swagger上显示测试,所以测试Header这也是必要的;

2.直接贴代码:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        //=====添加head参数start============================
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("Authorization").description("AccessToken令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        // =========添加head参数end===================
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.huajie.manage"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("后台管理服务API")
                .description("后台管理服务接口文档说明")
                .contact(new Contact("xx", "", "[email protected]"))
                .version("1.0")
                .build();
    }

    @Bean
    UiConfiguration uiConfig() {
        return new UiConfiguration(null, "list", "alpha", "schema",
                UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
    }
}

只需要在上面添加一部分就可以啦,还可以添加其它的字段

3.如何标示传入参数描述信息

 如果是一个Model的话,采用其提供的注解标识,如下:

a: 接口
@ApiOperation(value = "获取用户分页列表")
@PostMapping("/list")
public AjaxResult list(@RequestBody User user, @RequestBody PageDomain pageDomain) 
b.Model实体类
@ApiModel
public class User extends BaseEntity {
    /**
     * 用户ID
     */
    @ApiModelProperty(value = "用户ID")
    private Long userId;

c.效果:

SpringBoot2整合Swagger2添加Header头属性字段信息,与添加字段属性描述

 

备注:可能需要注意的是swagger自带的静态资源,可能会被拦截,如果被拦截的话所以要放行哦

/**
 * WebMvcConfig配置类
 *
 * @author : xxx
 * @date : 2019/1/17 08:44
 */
@Configuration
@EnableWebMvc
public class MannageWebMvcConfig implements WebMvcConfigurer {


    /**
     * 添加拦截器
     *
     * @author fangyi
     * @date 2019/1/17
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AccessAuthInterceptor()) // 添加的自定义拦截器
                .addPathPatterns("/**")
                .excludePathPatterns("/swagger-resources/**", "*.js", "/**/*.js", "*.css", "/**/*.css", "*.html", "/**/*.html");// 不拦截静态资源
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/"); // 静态资源的目录
    }

}

 

相关文章: