准备环境

application.yml

spring:
  application:
    name: knife4j-demo
knife4j:
  enable: true
  openapi:
    title: Knife4j官方文档
    description: "`我是测试`,**你知道吗**
    # aaa"
    email: xiaoymin@foxmail.com
    concat: 八一菜刀
    url: https://docs.xiaominfo.com
    version: v4.0
    license: Apache 2.0
    license-url: https://stackoverflow.com/
    terms-of-service-url: https://stackoverflow.com/
    group:
      test1:
        group-name: 分组名称
        api-rule: package
        api-rule-resources:
          - com.example.swaggertest

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>Swagger-Test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Swagger-Test</name>
    <description>Swagger-Test</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
            <version>4.4.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1-jre</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

目录配置

仅需三个文件即可实现无需其他配置类
image.png

@ApiOperationExtended

package com.example.swaggertest.annotaion;

import io.swagger.annotations.Extension;
import io.swagger.annotations.ExtensionProperty;
import io.swagger.annotations.ApiOperation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@ApiOperation(value = "", extensions = {
        @Extension(name = "price", properties = {
                @ExtensionProperty(name = "price", value = "")
        })
})
public @interface ApiOperationExtended {

    String value() default "";
    String price() default "";

}

ApiOperationExtendedPlugin

package com.example.swaggertest.plugin;


import com.example.swaggertest.annotaion.ApiOperationExtended;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Component;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
import springfox.documentation.swagger.common.SwaggerPluginSupport;

import java.util.Collections;
import java.util.Optional;

@Component
public class ApiOperationExtendedPlugin implements OperationBuilderPlugin {

    @Override
    public boolean supports(DocumentationType d) {
        return SwaggerPluginSupport.pluginDoesApply(d);
    }

    @Override
    public void apply(OperationContext context) {
        Optional<ApiOperation> methodAnnotation = context.findAnnotation(ApiOperation.class);
        if (methodAnnotation.isPresent()) {
            Optional<ApiOperationExtended> apiOperationExtended = context.findAnnotation(ApiOperationExtended.class);
            if (apiOperationExtended.isPresent()) {
                String price = apiOperationExtended.get().price();
                String value = apiOperationExtended.get().value();
                context.operationBuilder().extensions(Collections.singletonList(new springfox.documentation.service.StringVendorExtension("price", price)));
                context.operationBuilder().extensions(Collections.singletonList(new springfox.documentation.service.StringVendorExtension("summary", value)));
            }
        }
    }
}

Controller

package com.example.swaggertest.controller;

import com.example.swaggertest.annotaion.ApiOperationExtended;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 王成宇   2024/6/3 10:38
 */
@RestController
@Api(tags = "test")
public class TestController {

    @ApiOperationExtended(value = "测试新增注解",price = "99.99元/个")
    @GetMapping("/test")
    public String test() {
        return "test";
    }

}

实现效果

image.png

最后修改:2024 年 06 月 03 日
如果觉得我的文章对你有用,请随意赞赏