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
.parameterType("cookie")
.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);
}
@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/");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/api-docs","/swagger-ui.html");
}
}
swagger:
host: localhost:8080