当前位置:网站首页>Swagger简单使用

Swagger简单使用

2022-08-11 07:42:00 风铃峰顶

Swagger的Controller可以写@Api(tags = “导航名”),会显示在Swagger网页左侧导航里,下拉可以看到里面的接口。@ApiOperation(“接口名”)。
@ApiVersion({版本号,版本号})。在Swagger网页左侧上面搜索栏里可以搜索版本号。

@Configuration
@EnableSwagger2WebMvc
public class DocConfig implements InitializingBean {
    
private final OpenApiExtensionResolver openApiExtensionResolver;
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    public DocConfig(OpenApiExtensionResolver openApiExtensionResolver) {
    
        this.openApiExtensionResolver = openApiExtensionResolver;
    }
@Bean
    public Docket createBusinessRestApi() {
    
        String groupName = "";
        ApiInfo businessApiInfo = new ApiInfoBuilder()
                .title(groupName)
                .description("")
                .contact(new Contact("", "", ""))
                .version("1.0")
                .build();
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(groupName)
                .apiInfo(businessApiInfo)
                // 设置统一参数,如Header中的参数
                .globalOperationParameters(params())
                .select()
                .apis(RequestHandlerSelectors.basePackage(""))
                .paths(PathSelectors.any())
                .build()
                .extensions(openApiExtensionResolver.buildExtensions(groupName));
    }
    
private List<Parameter> params() {
    
        ParameterBuilder tokenParam = new ParameterBuilder();
        tokenParam.name("token").description("Token")
                .modelRef(new ModelRef("string")).parameterType("header").required(false).build();
                }
                return Lists.newArrayList(tokenParam.build());
}

原网站

版权声明
本文为[风铃峰顶]所创,转载请带上原文链接,感谢
https://blog.csdn.net/haoranhaoshi/article/details/126180839