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

Jax-RS Maven项目是否有编译时生成OpenAPI客户端存根的等效插件?

针对JAX-RS Maven项目的OpenAPI客户端存根生成方案

当然有,以下两种Maven插件方案可以实现编译阶段静态分析JAX-RS源码生成OpenAPI客户端存根,无需启动应用,和你提到的swagger-maven-plugin一样高效:

1. 组合使用 swagger-maven-plugin + OpenAPI Generator Maven Plugin

这是目前最成熟的方案:先用swagger-maven-plugin从JAX-RS源码生成OpenAPI契约文件,再用OpenAPI Generator基于该契约生成客户端存根,两者都绑定到compile阶段自动执行。

完整Maven配置示例

<!-- 第一步:从JAX-RS源码生成OpenAPI契约 -->
<plugin>
    <groupId>com.github.kongchen</groupId>
    <artifactId>swagger-maven-plugin</artifactId>
    <version>3.1.3</version>
    <configuration>
        <apiSources>
            <apiSource>
                <springmvc>false</springmvc>
                <locations>com.example.rest.resources</locations>
                <schemes>https</schemes>
                <info>
                    <title>project Service</title>
                    <version>v2</version>
                    <description>Description here.</description>
                    <contact>
                        <email>test@example.com</email>
                        <name>project Service</name>
                    </contact>
                </info>
                <!-- 生成OpenAPI json契约 -->
                <swaggerDirectory>${basedir}/target/generated-resources</swaggerDirectory>
            </apiSource>
        </apiSources>
    </configuration>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<!-- 第二步:基于生成的OpenAPI契约生成JAX-RS客户端存根 -->
<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>7.2.0</version>
    <executions>
        <execution>
            <id>generate-client</id>
            <phase>compile</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <!-- 指定第一步生成的OpenAPI契约路径 -->
                <inputSpec>${basedir}/target/generated-resources/swagger.json</inputSpec>
                <!-- 生成客户端的语言,这里选JAX-RS 2.0客户端 -->
                <generatorName>jaxrs-client</generatorName>
                <!-- 客户端代码输出目录 -->
                <output>${basedir}/target/generated-sources/openapi-client</output>
                <configOptions>
                    <!-- 指定客户端包名 -->
                    <packageName>com.example.client</packageName>
                    <!-- 是否仅生成接口,不生成实现类 -->
                    <interfaceOnly>false</interfaceOnly>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

关键说明

  • swagger-maven-plugin会在compile阶段扫描指定包下的JAX-RS接口,读取Swagger/OpenAPI注解生成规范文件swagger.json
  • OpenAPI Generator会紧接着读取该规范文件,生成可直接调用的JAX-RS客户端代码,默认输出到target/generated-sources/openapi-client,可通过IDE标记为源码目录

2. swagger-codegen-maven-plugin(旧版,兼容Swagger 2.0)

如果你的项目仍基于Swagger 2.0规范,可直接使用这个插件,它支持从JAX-RS源码直接生成客户端,但目前维护优先级低于OpenAPI Generator:

<plugin>
    <groupId>io.swagger.codegen.v3</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>3.0.46</version>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <!-- 指定JAX-RS源码路径 -->
                <inputSpec>${basedir}/src/main/java</inputSpec>
                <language>jaxrs-client</language>
                <output>${basedir}/target/generated-sources/swagger-client</output>
                <configOptions>
                    <packageName>com.example.client</packageName>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

注意事项

  • 确保你的JAX-RS接口已添加Swagger/OpenAPI注解(如@Api、@ApiOperation、@ApiParam等),插件依赖这些注解生成准确的客户端逻辑
  • 所有代码生成动作都会在Maven的compile阶段自动完成,无需手动触发或启动应用

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 00:45:26