Java后端开发——Swagger接口文档代码示例
package com.xolo.core.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.ParameterBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.schema.ModelRef; import springfox.documentation.service.*; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.List; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { ParameterBuilder param = new ParameterBuilder(); List<Parameter> params = new ArrayList<>(); param.name("Authorization").description("用户登录令牌") .modelRef(new ModelRef("string")).parameterType("header") .required(true).build(); params.add(param.build()); return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.xolo.core.controller")) .paths(PathSelectors.any()) .build() .globalOperationParameters(params) .ignoredParameterTypes(HttpServletResponse.class, HttpServletRequest.class) .apiInfo(new ApiInfoBuilder() .title("页面头") .description("页面描述") .contact(new Contact("创建人姓名", "阅读更多信息的页面地址", "联系邮箱地址")) .version("版本号") .build() ); } }
扫码关注公众号,查看更多精彩内容
本文来自博客园,作者:不是公子的小白,转载请注明原文链接:https://www.cnblogs.com/bobbycheng/p/11850280.html