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

本文介绍了如何在SpringBoot2中使用Swagger2添加Header头属性字段,并展示了添加字段属性描述的方法。通过代码示例,展示了在Swagger配置中添加Header信息,以及如何为Model参数添加描述,以完善接口文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在SpringCloud Zuul网关整合Swagger在网关swagger-ui.html查看各个服务的接口文档  的基础上补充添加头部信息,

当我们需要传递获取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", "", "xx@qq.com"))
                .version("1.0")
                .build();
    }

    @Bean
    UiConfiguration uiConfig() {
        return new UiConfiguration(true, false, -1, -1,
                ModelRendering.MODEL, true, DocExpansion.LIST, null, null,
                OperationsSorter.ALPHA, true, TagsSorter.ALPHA, null);
    }
}

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

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.效果:

 

备注:可能需要注意的是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/"); // 静态资源的目录
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值