【SpringBoot分别整合Swagger2.x与3版本】

SpringBoot整合Swagger

一、整合Swagger2

1、在pom.xml文件中引入依赖

  这里使用的是Swagger2.9版本依赖

 	<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>

2、关注配置文件application.yml

在整合Swagger2版本,需要由于Spring Boot 2.6及更高版本使用的是 PathPatternMatcher ,所以需要修改配置

spring:
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER

3、在项目中添加配置类SwaggerConfig.class

  重要注解 @EnableSwagger2

 	/**
 	* 构建api文档的详细信息
 	* @return
 	*/
  	private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 设置页面标题
                .title("Spring Boot集成 Swagger2接口总览")
                // 设置页面描述
                .description("这是一个简短的页面描述")
                // 设置联系方式
                .contact("空山万籁," + "CSDN:http://blog.csdn.net/KS_wl")
                // 设置版本
                .version("0.1")
                // 构建
                .build();
    }

	@Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                // 指定构建api文档的详细信息的方法:apiInfo() 
                .apiInfo(apiInfo())
                .select()
                // 指定要生成api接口的包路径,这里把controller作为包路径,生成controller中的所有接口
                .apis(RequestHandlerSelectors.basePackage("com.study.controller"))
                .paths(PathSelectors.any())
                .build();
    }

4、具体的Controller类

@Api(tags = "j简单测试接口")
@RestController
@RequestMapping("/swagger")
public class TestController {

    @GetMapping("/get/{id}")
    public JsonResult<User> getUserinof(@PathVariable @ApiParam(value = "用户唯一标识") int id) {
        User user = new User(1, "张三", "123456");
        return new JsonResult(user);
    }

    /*
        @Api 注解用于类上,表示标识这个类是 swagger 的资源。
        @ApiOperation 注解用于方法,表示一个 http 请求的操作。
        @ApiParam 注解用于参数上,用来标明参数信息。
    */
}


二、SpringBoot整合Swagger3

1、整合Swagger3

大致步骤与Swagger2相同,只有局部细节不同
依赖部分

<dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-boot-starter</artifactId>
      <version>3.0.0</version>
</dependency>

注解部分
  在SwaggerConfig的配置类上,改用了@EnableOpenApi,在下面的代码中 new Docket() 中修改参数为DocumentationType.OAS_30

@Configuration
@EnableSwagger2
public class SwaggerConfig {
	/*这是 Swagger2.x 的配置类*/
	/*省略内容部分*/
	 @Bean
    public Docket createRestApi(){
    	/*这里使用的DocumentationType.SWAGGER_2  */
        return new Docket(DocumentationType.SWAGGER_2).....;
            
    }
}

@Configuration
@EnableOpenApi
public class SwaggerConfig {
	/*这是 Swagger3.0 的配置类*/
	/*省略内容部分*/
	 @Bean
    public Docket createRestApi(){
    	/*这里使用的DocumentationType.OAS_30 */
        return new Docket(DocumentationType.OAS_30)....;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,你想要了解如何在Spring Boot 2.7.9中整合Swagger2并使用com.github.xiaoymin的knife4j扩展库,是吗?我可以为你提供一些步骤。 首先,你需要在你的pom.xml文件中添加以下依赖: ```xml <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> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.8</version> </dependency> ``` 然后,你需要在你的Spring Boot配置类中添加以下内容: ```java @Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Swagger2接口文档") .description("Swagger2接口文档") .termsOfServiceUrl("http://localhost:8080/") .version("1.0") .build(); } } ``` 这个配置类将创建一个名为"Swagger2接口文档"的文档,并扫描com.example.demo.controller包中的所有控制器。 最后,你可以在你的浏览器中访问http://localhost:8080/doc.html来查看生成的文档。 如果你想自定义Swagger UI的主题,你可以在application.properties中添加以下配置: ```properties # Swagger UI主题 knife4j.swaggerui.path=/doc.html knife4j.swaggerui.title=Swagger2接口文档 knife4j.swaggerui.description=Swagger2接口文档 knife4j.swaggerui.version=1.0 knife4j.swaggerui.contact.name=联系人姓名 knife4j.swaggerui.contact.email=联系人邮箱 knife4j.swaggerui.contact.url=联系人网址 knife4j.swaggerui.license.name=许可证名称 knife4j.swaggerui.license.url=许可证网址 knife4j.swaggerui.enable=true # 配置主题 knife4j.swaggerui.theme=flattop ``` 这将启用knife4j并使用flattop主题。 希望这些步骤对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值