Springboot 整合springfox

1 导包

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

版本要选择最新的3.x.x版本

2 配置

创建一个java配置类SpringfoxConfiguration配置Docket(文档信息)

@Configuration
public class SpringfoxConfiguration {
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.OAS_30)
                // 文档名
                .groupName("springfox-hello")
                // 添加info信息
                .apiInfo(info())
                // 开启选择器
                .select()
                // 选择要扫描的包,包下的所有控制器都会陪扫描
                .apis(RequestHandlerSelectors.basePackage("com.aion.springfoxdemo.controller"))

                // 选择要匹配的路径
                .paths(PathSelectors.ant("/student"))
                // 匹配所有的路径
//                .paths(PathSelectors.any())
                .build();
    }

    // 创建info信息
    private ApiInfo info(){
        return new ApiInfoBuilder()
                .title("springfox test api")
                .description("这只是用来测试的简单api")
                // 添加可联系人信息
                .contact(new Contact( "aion", "http://aion.com", "aion@qq.com"))
                .version("0.1")
                .build();
    }
}

3 创建控制器

package com.aion.springfoxdemo.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Tag(name = "student", description = "学生管理")
public class StudentController {

    @Tag(name = "student")
    @Operation(summary = "一个只会返回student的api")
    @ApiResponse(responseCode = "200", description = "成功使用了此接口")
    @GetMapping("/student")
    public String get(){
        return "student";
    }
}

4 查看api文档

  • 启动项目
  • 在浏览器中输入http://localhost:8080/swagger-ui/index.htm就能查看api文档

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于Spring BootSpringfox(以前称为Swagger)的整合,可以按照以下步骤进行: 1. 在项目的pom.xml文件中添加Springfox依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 2. 在Spring Boot的启动类上添加`@EnableSwagger2`注解,该注解是Springfox的配置开关: ```java import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2 @SpringBootApplication public class YourApplication { // ... } ``` 3. 创建一个配置类,用于配置Swagger的基本信息和API文档的生成规则: ```java 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 @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.your.package")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Your API Title") .description("Your API Description") .version("1.0.0") .build(); } } ``` 确保将上述代码中的`com.your.package`替换为你项目中的包路径。 4. 启动应用程序,并访问`http://localhost:8080/swagger-ui.html`可以查看自动生成的API文档。 这样,你就成功地将Spring BootSpringfox整合起来了。你可以使用Swagger注解来配置API文档和参数信息,具体用法可以参考Springfox的文档。 希望对你有所帮助!如果你有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值