spring:3.x
无拦截器
参考——
https://blog.csdn.net/qq_55269149/article/details/137285469
(下边有2.x的配置,不过没试过
配置
<!-- 生成接口文档-->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
<version>2.5.0</version>
</dependency>
代码
SwaggerConfig.java
package com.example.config; import io.swagger.v3.oas.models.ExternalDocumentation; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Contact; import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.info.License; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class SwaggerConfig { @Bean public OpenAPI springShopOpenAPI() { return new OpenAPI() .info(new Info().title("Spring Boot 中使用 Swagger UI 构建 RESTful API") .contact(new Contact()) .description("信息管理平台提供的 RESTful API") .version("v1.0.0") .license(new License().name("Apache 2.0").url("http://springdoc.org"))) .externalDocs(new ExternalDocumentation() .description("外部文档") .url("https://springshop.wiki.github.org/docs")); } }
application.properties中配置
# 开启OpenApi接口 springdoc.api-docs.enabled: true # 开启swagger界面,依赖OpenApi,需要OpenApi同时开启 springdoc.swagger-ui.enabled: true
访问
http://localhost:8080/swagger-ui.html
效果:
——————2.x (没试过)
参考——
https://www.bilibili.com/video/BV1nV4y1s7ZN/?p=5&spm_id_from=pageDriver&vd_source=c2fca9b4cf27a6496643dde809415b05
配置
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
需要的代码相关
SwaggerConfig.java
package com.example.config; 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 //告诉spring这是一个配置类 @EnableSwagger2 //启用swagger2功能 public class SwaggerConfig { /* 配置swagger2相关的bean */ @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() // com包下的所有api都较给swagger2管理 .apis(RequestHandlerSelectors.basePackage("com")) .paths(PathSelectors.any()).build(); } /* 此处主要是api文档页面显示信息 */ private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("演示项目api") //标题 .description("演示项目") //描述 .version("1.0") //版本 .build(); } }
————注意
如果springboot是2.6以后的版本,需要在application.properties中加入以下配置
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
访问
http://localhost:8080/swagger-ui.html
标签:springfox,new,生成,依赖,自动,io,org,import,swagger From: https://www.cnblogs.com/yansans/p/18340405