package com.gyee.consumer.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import javax.annotation.Resource; /** * Swagger 配置文件 * @ClassName: Swagger * @Description: 配置文件 * @author gyee * @date 2018年6月3日 * */ @Configuration(value="true") @EnableSwagger2 //启动swagger注解 启动服务,浏览器输入"http://localhost:8170/swagger-ui.html" public class Swagger implements WebMvcConfigurer { @Resource private V2Config v2Config; @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .host(v2Config.getSwaggerip()) // 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息) .apiInfo(apiInfo()) // 设置哪些接口暴露给Swagger展示 .select() // 扫描所有有注解的api,用这种方式更灵活 .apis(RequestHandlerSelectors.basePackage("com.gyee")) // 扫描指定包中的swagger注解 //.apis(RequestHandlerSelectors.basePackage("com.gyee.frame.controller")) // 扫描所有 .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() //设置标题 .title("API文档") //描述 .description("项目") //作者信息 //.contact(new Contact(v2Config.getName(), null, V2Config.getEmail_account())) //服务条款URL .termsOfServiceUrl("") //版本 .version("") .build(); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // 解决静态资源无法访问 registry.addResourceHandler("/**").addResourceLocations("classpath:/static/"); // 解决swagger无法访问 registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); // 解决swagger的js文件无法访问 registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } }