摘要
这是一个基于Spring Boot和Swagger2的配置类。它主要完成以下任务:
- 配置Swagger2的Docket实例
- 配置Swagger UI的资源映射和路径映射
- 加载yml配置文件中的Swagger host信息
Docket实例是Swagger2中最重要的类之一。它负责扫描指定包中的RequestMapping注解,并生成Api文档。在这个配置类中,我们将使用它来扫描net.ameizi包中的所有RequestMapping注解。
在yml配置文件中,我们定义了Swagger host信息,用于指定生成的Api文档所在的主机和端口。在这里,我们将Swagger host指定为localhost:8080。由于Swagger文档是以Api文档的形式生成的,因此它需要与接口服务运行在同一主机和端口上。
1.java配置类
package net.ameizi.config;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Value;
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.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
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.Contact;
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.List;
@Configuration
@EnableSwagger2
public class SwaggerConfiguration extends WebMvcConfigurerAdapter {
@Value("${swagger.host}")
private String swaggerHost;
ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("amz")
.description("基于shiro和jwt的前后端分离权限系统")
.termsOfServiceUrl("")
.version("1.0.0")
.contact(new Contact("", "", ""))
.build();
}
@Bean
public Docket createRestApi() {
ParameterBuilder builder = new ParameterBuilder();
Parameter parameter = builder
// 从cookie中获取token
.parameterType("cookie") //参数类型支持header, cookie, body, query etc
.name("token") //参数名
.defaultValue("") //默认值
.description("请输入token")
.modelRef(new ModelRef("string")) //指定参数值的类型
.required(false).build(); //非必需,这里是全局配置,然而在登陆的时候是不用验证的
List<Parameter> parameters = Lists.newArrayList(parameter);
return new Docket(DocumentationType.SWAGGER_2)
.host(this.swaggerHost)
.select()
.apis(RequestHandlerSelectors.basePackage("net.ameizi"))
.paths(PathSelectors.any())
.build()
.apiInfo(this.apiInfo())
.globalOperationParameters(parameters);
}
/**
* swagger ui资源映射
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars*")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
/**
* swagger-ui.html路径映射,浏览器中使用/api-docs访问
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/api-docs","/swagger-ui.html");
}
}
2.yml配置
swagger:
host: localhost:8080