當(dāng)我們在使用Spring MVC寫接口的時候,為了生成API文檔,為了方便整合Swagger,都是用這個SpringFox的這套封裝。但是,自從2.9.2版本更新之后,就一直沒有什么動靜,也沒有更上Spring Boot的大潮流,有一段時間還一直都是寫個配置類來為項(xiàng)目添加文檔配置的。為此,之前就造了這么個輪子:
也沒什么難度,就是造的早,所以得到了不少Star?,F(xiàn)在SpringFox出了一個starter,看了一下功能,雖然還不完美,但相較于之前我們自己的輪子來說還是好蠻多的。來看看這個版本有些什么亮點(diǎn):
Spring 5,Webflux 支持(僅請求映射支持,尚不支持功能端點(diǎn))
Spring Integration 支持
Spring Boot 支持 springfox-boot-starter 依賴性(零配置,自動配置支持)
具有自動完成功能的文檔化配置屬性
更好的規(guī)范兼容性
支持 OpenApi 3.0.3
幾乎零依賴性(唯一需要的庫是 spring-plugin、pswagger-core)
現(xiàn)有的 swagger2 注釋將繼續(xù)有效,并豐富 open API 3.0 規(guī)范
對于這次的更新,我覺得比較突出的幾點(diǎn):Webflux的支持,目前的輪子就沒有做到;對OpenApi 3的支持;以及對Swagger 2的兼容(可以比較方便的做升級了)。
錯誤行代碼:
Cause by:
Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
錯誤原因:Springfox使用的路徑匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher
解決方案:
1.簡單粗暴:升級spring-boot-starter-parent版本
?<parent>
? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? <artifactId>spring-boot-starter-parent</artifactId>
? ? ? ? <version>2.5.6</version>
? ? ? ? <relativePath/> <!-- lookup parent from repository -->
? ? </parent>
2.高級感:在application.yaml里配置:spring.mvc.pathmatch.matching-strategy: ANT_PATH_MATCHER
spring: mvc: pathmatch: matching-strategy: ant_path_matcher
接下來進(jìn)入主題:上手
第一步導(dǎo)入依賴
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
第二步在主啟動類開啟支持
文章來源:http://www.zghlxwxcb.cn/news/detail-435786.html
?訪問http://localhost:8080/doc.html即可 默認(rèn)ui就是bootstarp-ui。文章來源地址http://www.zghlxwxcb.cn/news/detail-435786.html
可以添加自定義設(shè)置
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//為當(dāng)前包下的controller生成api文檔
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
//設(shè)置文檔信息
return new ApiInfoBuilder()
.title("小7接口文檔")
.description("小7接口文檔")
.contact(new Contact("李佳琪", "http:localhost:8080/doc.html",
"1004102689@qq.com"))
.version("1.0")
.build();
}
/**
* 解決swagger在springboot2.7以后的空指針異常
*/
@Bean
public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
}
return bean;
}
private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
List<T> copy = mappings.stream()
.filter(mapping -> mapping.getPatternParser() == null).toList();
mappings.clear();
mappings.addAll(copy);
}
@SuppressWarnings("unchecked")
private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
try {
Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
assert field != null;
field.setAccessible(true);
return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
};
}
}
到了這里,關(guān)于springboot生成接口文檔的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!