springboot は swagger3 と knife4j を統合します

頼る

 <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-boot-starter</artifactId>
            <version>3.0.3</version>
</dependency>

application.yml 構成

  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher ## 解决Failed to start bean ‘documentationPluginsBootstrapper‘ 问题
knife4j:
  enable: true

swagger 構成クラス

package com.example.springbooth2.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
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.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@EnableOpenApi//Swagger 开启生成接口文档功能
public class SwaggerConfig {
    
    

    /**
     * ture 启用Swagger3.0, false 禁用
     */
    @Value("${knife4j.enable}")
    private Boolean enable;

    @Bean
    Docket docket() {
    
    
        return new Docket(DocumentationType.OAS_30)
                //配置网站的基本信息
                .apiInfo(apiInfo())
                .enable(enable)
                .select()
                //指定接口的位置
                // RequestHandlerSelectors.basePackage("com.example.springbooth2.controller") 接口的包所在路径
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 指定路径处理PathSelectors.any()代表所有的路径
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
    
    
        return new ApiInfoBuilder()
                .title("swagger3") //接口文档标题
                .description("接口文档") //接口文档描述
                //作者信息
                // new Contact("作者","作者URL","作者Email")
                .contact(new Contact("jane", "http://www.baidu.com", "[email protected]"))
                .version("1.0")//版本
                .build();
    }
}

コントローラ

package com.example.springbooth2.controller;

import com.example.springbooth2.config.ResponseResult;
import com.example.springbooth2.entity.User;
import com.example.springbooth2.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/user")
public class UserController {
    
    

    @Resource
    private UserService userService;

    @ApiOperation("添加用户")
    @ApiImplicitParams({
    
     // 参数名称
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "userName", value = "用户名",  required = true)
    })
    @PostMapping("add")
    public User add(User user) {
    
    
        userService.addUser(user);
        return user;
    }

    @ApiOperation("查询所有用户")
    @GetMapping("list")
    public ResponseResult<User> list() {
    
    
        return ResponseResult.success(userService.list());
    }

    @GetMapping("/findOne")
    public ResponseResult<User> findOne() {
    
    
        return ResponseResult.success(userService.findById(1));
    }
}

テスト

knife4j アクセスアドレス http://localhost:9090/doc.html
ここに画像の説明を挿入

swagger アクセスアドレス http://localhost:9090/swagger-ui/index.html
ここに画像の説明を挿入

よくある注釈を闊歩する

@アピ

要求されたクラスで使用
@Api(tags = "このクラスの役割の説明")

@ApiOperation

@ApiOperation(value="メソッド変更機能", note="備考")

@ApiImplicitParams

@ApiImplicitParams: リクエスト メソッドで使用され、パラメーター説明のセットを示し
ますパラメータを渡す必要がありますparamType: パラメータヘッダーを配置する場所 --> リクエストパラメータの取得: @RequestHeaderクエリ --> リクエストパラメータの取得: @RequestParamパス (Restful インターフェイスの場合) -> リクエストパラメータの取得: @PathVariable dataType: パラメータの型,デフォルト文字列、その他の値 dataType=“Integer” defaultValue: パラメーターのデフォルト値複数の @ApiImplicitParam アノテーションを 1 つの @ApiImplicitParams アノテーションに配置する必要があります@ApiImplicitParam アノテーションはパラメーターが必須であることを指定できますが、@ を置き換えることはできませんRequestParam(required = true) ,前者は Swagger フレームワークでのみ必要です. Swagger が破棄される場合、この制限は役に立たないため、開発者が必須のパラメーターを指定する必要がある場合は、 @RequestParam(required = true) アノテーションがstill 省略できません。











@ApiModel

パラメーターがオブジェクト (上記の更新インターフェイスなど) の場合、パラメーターの説明もエンティティ クラスに配置できます。たとえば、次のコード
@ApiModelProperty: 属性で使用され、クラスの属性を記述します。

@ApiModel(value = "用户对象",description = "用户表")
public class User {
    
    
    @Id
    @ApiModelProperty(value = "id",required = true)
    private int userId;
    @ApiModelProperty(value = "用户名称",required = true)
    private String userName;
}

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/Java_Fly1/article/details/127751010