卜若的代码笔记系列-Web系列-SpringBoot-第十一章:Swagger环境搭建-3211
背景:我们遇到一个问题,在团队开发的时候,我们需要共享api
信息(api,也就是应用程序接口,之前主城讲那你写个什么什么
接口,其实讲的就是去实现这样一个api,but,我之前一直理解为时
函数,emmmm,这就很尴尬了)。然后怎么共享api,这是一个问题,
所以,在这个问题的背景下,我们提出了swagger
ps:嘤嘤嘤?拜托,网上的大神们,能不能写详细点,真的好难找,不想写详细点就别写好吗,怕您累着。
1.首先,我们想使用的swagger是一个插件,所以我们要通过maven去添加相关依赖。
在pom里面添加
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
这样maven会自动下载一些swagger的相关依赖。
2.需要注册这个swagger的相关信息,怎么注册呢,当然是创建一个类,
通过@Configuration这个标签去注册
通过@EnableSwagger2去启用所有的swagger
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("brTest.example.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SpringBoot&Swagger")
.description("这是一个swagger的测试")
.termsOfServiceUrl("https://blog.****.net/qq_37080133")
.version("1.0")
.build();
}
}
3.你注册了这个信息之后,你得去启用,真的是感谢大神的博客,我找了两个小时,心态炸了,网上千篇一律,真特么恶心,你特么抄也抄详细点行吗?
https://blog.****.net/u013128651/article/details/78864876
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public class Swagger2Config
{
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
}
4.这样,如果你还进不去你就留言 ,我帮你解决,最近我一直在有空
5.大佬们,真的,你不想写详细点让菜鸟能够看得懂就别写,会的人不看,好气,浪费时间是吗?