本文主要是介绍【Swagger】SpringBoot整合Swagger,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、搭建Swagger组件
1. 引入Swagger
快速在SpringBoot(版本:2.6.3)项目中引入Swagger相应pom.xml:
<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version>
</dependency>
2. 添加Swagger配置文件
@Configuration
@EnableWebMvc
public class SwaggerConfig {Boolean swaggerEnabled=true;@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo())// 是否开启.enable(swaggerEnabled).select()// 扫描的路径包.apis(RequestHandlerSelectors.basePackage("com.king.springbootdemo"))// 指定路径处理PathSelectors.any()代表所有的路径.paths(PathSelectors.any()).build().pathMapping("/");}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("SpringBoot-Swagger3集成和使用-demo示例").description("springboot | swagger")// 作者信息.contact(new Contact("King", "", "")).version("1.0.0").build();}
}
3. 创建一个Controller
@RestController
@Api(value = "desc of class")
public class DemoController {@GetMapping("/demo")@ApiOperation(value = "第一个接口demo", notes = "")public String demo() {return "hello demo !!";}@GetMapping("/test")@ApiOperation(value = "第二个接口test", notes = "")public String test(@ApiParam(value = "姓名",required=true) @RequestParam String name) {return "hello :" + name;}
}
4. SpringBoot启动类添加加载配置
@EnableOpenApi
5. 配置完成后Swagger界面
路径:http://localhost:8080/swagger-ui/index.html
6. 使用更美观第三方UI
- 引入第三方pom配置
PS:swagger-bootstrap-ui
作者已经暂停更新,可以使用knife4j-spring-ui
。
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>swagger-bootstrap-ui</artifactId><version>1.9.6</version>
</dependency>
- 使用自定义配置,需要将原来的
@EnableWebMvc
去掉
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOriginPatterns("*").allowedMethods("PUT", "DELETE", "GET", "POST", "OPTIONS").allowCredentials(true).maxAge(3600);}@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}
}
- 第三方使用界面,
http://localhost:8080/doc.html
二、搭建过程出现异常
1. 引入Swagger启动报错,Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
两种解决方案:
- 添加注解配置,
@EnableWebMvc
- 配置文件增加配置,
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-02-09 15:38:15.961 ERROR 13208 --- [ main] o.s.boot.SpringApplication : Application run failedorg.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerExceptionat 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_77]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.king.springbootdemo.SpringbootDemoApplication.main(SpringbootDemoApplication.java:12) [classes/:na]
Caused by: java.lang.NullPointerException: nullat springfox.documentation.spring.web.WebMvcPatternsRequestConditionWrapper.getPatterns(WebMvcPatternsRequestConditionWrapper.java:56) ~[springfox-spring-webmvc-3.0.0.jar:3.0.0]at springfox.documentation.RequestHandler.sortedPaths(RequestHandler.java:113) ~[springfox-core-3.0.0.jar:3.0.0]at springfox.documentation.spi.service.contexts.Orderings.lambda$byPatternsCondition$3(Orderings.java:89) ~[springfox-spi-3.0.0.jar:3.0.0]at java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:469) ~[na:1.8.0_77]at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) ~[na:1.8.0_77]at java.util.TimSort.sort(TimSort.java:220) ~[na:1.8.0_77]at java.util.Arrays.sort(Arrays.java:1512) ~[na:1.8.0_77]
感谢阅读,下次再见。ヾ( ̄▽ ̄)ByeBye!
这篇关于【Swagger】SpringBoot整合Swagger的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!