第一步 使用idea创建SpringBoot项目该系列专注于后端代码,前端等我有空再写 或者由读者们自由发挥
开发环境:java8,idea2022.3
现在网上很多的项目使用的技术的版本都比较老 我会尽量的使用比较新的版本
最终图片为
项目介绍 项目启动然后点next 下一步
SpringBoot 集成 Swagger3项目启动后 我们访问本机的8080端口号就可以访问这个springboot后端项目了
引入Swagger3依赖在实际开发前后端项目中,前端和后端是分开的 不在一台电脑上也不是同一个人进行开发。后端需要将可以完成某些系统业务的接口告诉给前端人员 前端人员通过调用接口获取数据并渲染到页面 完成业务功能。因此后端需要一个接口文档 列出后端项目的所有接口。Swagger3 可以帮助我们完成这个工作。有兴趣的读者可以自行百度了解一下。
io.springfox
springfox-boot-starter
3.0.0
编写配置文件
编写Swagger属性映射类
这里需要注意 : 该类放在config包下 config包与主启动类同级
详细代码
common-long3 依赖
org.apache.commons
commons-lang3
package com.example.fileserver.config;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootVersion;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
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.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import java.lang.reflect.Field;
import java.util.*;
@Configuration
public class SwaggerConfig implements WebMvcConfigurer {
private final SwaggerProperties swaggerProperties;
public SwaggerConfig(SwaggerProperties swaggerProperties) {
this.swaggerProperties = swaggerProperties;
}
@Bean
public Docket docket(){
return new Docket(DocumentationType.OAS_30)
.pathMapping("/")
// 定义是否开始swagger
.enable(swaggerProperties.getEnable())
// 将api的元信息设置为包含再json ResourceList响应中
.apiInfo(apiInfo())
// 接口调试地址
.host(swaggerProperties.getTryHost())
// 选择哪些接口作为swagger的doc发布
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.protocols(newHashSet("https","http"))
// 授权信息设置 必要的header token等认证信息
.securitySchemes(securitySchemes())
// 授权信息全局应用
.securityContexts(securityContexts());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title(swaggerProperties.getApplicationName() + " Api Doc")
.description(swaggerProperties.getApplicationDescription())
.contact(new Contact("author", null, "2510043259@gmail.com"))
.version("Application Version: " + swaggerProperties.getApplicationVersion() + ", Spring Boot Version: " + SpringBootVersion.getVersion())
.build();
}
private List securitySchemes() {
ApiKey apiKey = new ApiKey("BASE_TOKEN", "token", In.HEADER.toValue());
return Collections.singletonList(apiKey);
}
private List securityContexts() {
return Collections.singletonList(
SecurityContext.builder()
.securityReferences(Collections.singletonList(new SecurityReference("BASE_TOKEN", new AuthorizationScope[]{new AuthorizationScope("global", "")})))
.build()
);
}
@SafeVarargs
private final Set newHashSet(T... ts) {
if (ts.length > 0) {
return new LinkedHashSet<>(Arrays.asList(ts));
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public void addInterceptors(InterceptorRegistry registry) {
try {
Field registrationsField = FieldUtils.getField(InterceptorRegistry.class, "registrations", true);
List registrations = (List) ReflectionUtils.getField(registrationsField, registry);
if (registrations != null) {
for (InterceptorRegistration interceptorRegistration : registrations) {
interceptorRegistration
.excludePathPatterns("/swagger**/**")
.excludePathPatterns("/webjars/**")
.excludePathPatterns("/v3/**")
.excludePathPatterns("/doc.html");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
swagger示例
编写示例接口类
详细代码
package com.example.fileserver.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = "示例API")
@RestController
@RequestMapping("/")
public class IndexController {
@ApiOperation("示例")
@GetMapping("index")
public String index(){
return "hello world";
}
}
启动项目
报错:Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
修改配置文件
再次启动项目 访问localhost:8080/swagger-ui/index.html 可以打开接口文档页面



