一、简介二、配置Swagger
1、添加Swagger依赖2、创建Swagger配置类 三、访问http://localhost:8090/swagger-ui.html/
一、简介在项目开发中,一般都是前后端分离,所有前后端工程师需要共同定义接口,编写接口文档,之后根据文档进行开发和维护。
为了编写和维护稳定,可以使用Swagger来编写API接口文档,提高效率
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
2、创建Swagger配置类
在spirng boot集成时,放在与springbootApplication的同级目录下
package com.atmae.agriculture;
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 Swagger2 {
@Bean
public Docket createRestApi(){
return new Docket(documentationType.SPRING_WEB)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.atmae.agriculture.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("系统开发 API")
.description(("这是系统开发API 前后端共同定义"))
.termsOfServiceUrl("http://localhost:8090/")
.version("1.0")
.build();
}
}
注意:如果报一下错误,则说明你的Springboot 版本过高。
笔者使用的 2.6.4 换成了 2.5.1 错误就没了
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException三、访问http://localhost:8090/swagger-ui.html/
端口号根据自己的程序来确定。
注意:如果设置了访问权限 一定要给swagger的资源放行



