赞
踩
<!--swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> <exclusions> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> </exclusion> </exclusions> </dependency> <!--knife4j--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.4</version> </dependency>
# Swagger配置
swagger.enabled=true
代码示例:
package com.xxx;【修改路径】 import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; import org.springframework.beans.factory.annotation.Value; 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.ApiInfo; import springfox.documentation.service.Parameter; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import java.util.List; /** * Swagger2的接口配置 * * @author gxl */ @Configuration @EnableSwagger2 @EnableKnife4j public class SwaggerConfig { /** * 是否开启swagger */ @Value("${swagger.enabled}") private boolean enabled; /** * 创建API */ @Bean public Docket createRestApi() { ParameterBuilder ticketPar = new ParameterBuilder(); List<Parameter> pars = new ArrayList<Parameter>(); //Token 以及Authorization 为自定义的参数,session保存的名字是哪个就可以写成那个 ticketPar.name("Authorization").description("授权") .modelRef(new ModelRef("string")).parameterType("header") //header中的ticket参数非必填,传空也可以 .required(false).build(); //根据每个方法名也知道当前方法在设置什么参数 pars.add(ticketPar.build()); return new Docket(DocumentationType.SWAGGER_2) .enable(enabled) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.gxl")) .paths(PathSelectors.any()) .build() .globalOperationParameters(pars); } /** * 添加摘要信息 */ private ApiInfo apiInfo() { // 用ApiInfoBuilder进行定制 return new ApiInfoBuilder() // 设置标题 .title("boot项目_接口文档") // 描述 .description("boot项目服务") // 版本 .version("版本号:" + "1.0") .build(); } }
代码示例:
package com.xxx;【修改路径】 import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /** * 拦截器配置 * @author gxl */ @Configuration public class WebMvcConfigurer extends WebMvcConfigurationSupport { /** * 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源 * * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations( "classpath:/static/"); registry.addResourceHandler("swagger-ui.html").addResourceLocations( "classpath:/META-INF/resources/"); registry.addResourceHandler("doc.html").addResourceLocations( "classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations( "classpath:/META-INF/resources/webjars/"); super.addResourceHandlers(registry); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。