概述
swagger整合springboot2时大致分为两种情况分别是2.6.2之前和2.6.2-3.0版本的差异
- 1.整合2.6.3-3.0
- 抓重点来了:::::以下这种报错处理方法
- 2.第二种整合
1.整合2.6.3-3.0
- pom文件
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.7</version>
</dependency>
<!-- swagger增强文档-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.3/** 这里是[2.6.3-3.0)*/</version>
</dependency>
- 配置类
@Configuration
@EnableSwagger2
@EnableKnife4j
public class Swagger2 extends WebMvcConfigurationSupport {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//创建改api的基本信息
.select()
//select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现
.apis(RequestHandlerSelectors.basePackage("这里是你controller的reference例:com.xx.controller"))
.paths(PathSelectors.any())
.build()
/*.securitySchemes(securitySchemes())
.securityContexts(securityContexts())*/;
}
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("测试swaggerAPI")
.description("使用swagger让查看接口更方便")
.termsOfServiceUrl("www.xxx.com")
.version("1.0.0")
.build();
}
/**
* 设置静态资源映射
* @param registry
*/
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
/**
* 扩展mvc框架的消息转换器
* @param converters
*/
@Override
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//创建消息转换器对象
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
//设置对象转换器,底层使用Jackson将Java对象转为json
messageConverter.setObjectMapper(new JsonMapper()/*new ObjectMapper(new JsonFactory())*/);
//将上面的消息转换器对象追加到mvc框架的转换器集合中
converters.add(0,messageConverter);
}
}
以上代码访问http://127.0.0.1:8080/swagger-ui/index.html会有问题但是http://127.0.0.1:8080/doc.html是可以正常访问的
抓重点来了:::::以下这种报错处理方法
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-08-13 19:36:22.552 ERROR 11080 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.15.jar:5.3.15]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.15.jar:5.3.15]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.15.jar:5.3.15]
at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_311]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.15.jar:5.3.15]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.15.jar:5.3.15]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.15.jar:5.3.15]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.15.jar:5.3.15]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) [spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414) [spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) [spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.6.3.jar:2.6.3]
at com.hyx.rg.swaggertest.SwaggerTestApplication.main(SwaggerTestApplication.java:10) [classes/:na]
Caused by: java.lang.NullPointerException: null
这种情况就有很多解决办法
-
按照我上面整合访问(http://127.0.0.1:8080/doc.html)则不会出现问题
-
如果下面这种整合访问(http://127.0.0.1:8080/doc.html 或 http://127.0.0.1:8080/swagger-ui/index.html)方法需要在配置文件application.properties加入
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
2.第二种整合
- pom文件
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring</artifactId>
<version>3.0.2</version>
</dependency>
<!-- 比较完善的一种-->
<!-- <dependency>-->
<!-- <groupId>com.github.xiaoymin</groupId>-->
<!-- <artifactId>knife4j-spring-boot-starter</artifactId>-->
<!-- <version>3.0.2</version>-->
<!-- </dependency>-->
<!-- 或者是下面这种-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-ui</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.3/** 这里是无所谓*/</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.72</version>
</dependency>
- 配置类
@Configuration
@EnableOpenApi
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
//创建改api的基本信息
.select()
//select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现
.apis(RequestHandlerSelectors.basePackage("com.xxx.controller"))
.paths(PathSelectors.any())
.build()
/*.securitySchemes(securitySchemes())
.securityContexts(securityContexts())*/;
}
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("测试swaggerAPI")
.description("使用swagger让查看接口更方便")
.termsOfServiceUrl("www.xxx.com")
.version("1.0.0")
.build();
}
}
最后
以上就是贪玩棒球为你收集整理的【swagger整合springboot2】1.整合2.6.3-3.02.第二种整合的全部内容,希望文章能够帮你解决【swagger整合springboot2】1.整合2.6.3-3.02.第二种整合所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复