You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Swagger使生成的UI接口顺序与Java控制器类一致

如何配置Swagger使UI接口顺序与控制器方法定义顺序一致

可以实现,根据你使用的Swagger版本,有两种常见配置方式:

一、Swagger 2.x(Springfox实现)

方式1:全局自动按方法定义顺序排序

在Swagger配置类中,给Docket设置自定义排序逻辑,通过反射获取方法在控制器类中的定义顺序来排序:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("你的控制器包路径"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(new ApiInfoBuilder().title("API文档").version("1.0").build())
                .operationOrdering((left, right) -> {
                    Method leftMethod = ((HandlerMethod) left.getHandlerMethod()).getMethod();
                    Method rightMethod = ((HandlerMethod) right.getHandlerMethod()).getMethod();
                    return Integer.compare(getMethodPosition(leftMethod), getMethodPosition(rightMethod));
                });
    }

    private int getMethodPosition(Method method) {
        Method[] methods = method.getDeclaringClass().getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].equals(method)) {
                return i;
            }
        }
        return Integer.MAX_VALUE;
    }
}

方式2:逐个方法指定顺序

给每个接口方法的@ApiOperation注解添加position属性,按方法定义顺序赋值:

@RestController
public class SomeController {
    @GetMapping("/method1")
    @ApiOperation(value = "方法1", position = 1)
    public String method1() {
        return "result1";
    }

    @GetMapping("/method2")
    @ApiOperation(value = "方法2", position = 2)
    public String method2() {
        return "result2";
    }
}

二、Swagger 3.x(SpringDoc OpenAPI实现)

方式1:全局自动按方法定义顺序排序

自定义OpenApiCustomiser来实现排序逻辑:

@Configuration
public class SpringDocConfig {
    @Bean
    public OpenApiCustomiser operationOrderCustomiser() {
        return openApi -> {
            openApi.getPaths().values().forEach(pathItem -> {
                pathItem.readOperations().sort((op1, op2) -> {
                    Method method1 = ((HandlerMethod) op1.getExtensions().get("handlerMethod")).getMethod();
                    Method method2 = ((HandlerMethod) op2.getExtensions().get("handlerMethod")).getMethod();
                    return Integer.compare(getMethodPosition(method1), getMethodPosition(method2));
                });
            });
        };
    }

    private int getMethodPosition(Method method) {
        Method[] methods = method.getDeclaringClass().getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].equals(method)) {
                return i;
            }
        }
        return Integer.MAX_VALUE;
    }
}

方式2:逐个方法指定顺序

给每个接口方法的@Operation注解添加order属性:

@RestController
public class SomeController {
    @GetMapping("/method1")
    @Operation(summary = "方法1", order = 1)
    public String method1() {
        return "result1";
    }

    @GetMapping("/method2")
    @Operation(summary = "方法2", order = 2)
    public String method2() {
        return "result2";
    }
}

两种方式各有优劣:全局配置适合接口数量多的场景,不用逐个修改方法;逐个指定顺序更灵活,可按需调整个别接口的位置。

内容的提问来源于stack exchange,提问作者user19282140

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 18:40:29