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

如何为openapi-generator-maven-plugin生成的REST接口添加异常抛出声明

问题

在SpringBoot应用中使用openapi-generator-maven-plugin从YAML文件生成Java类时,希望所有生成的REST接口方法签名都包含throws CustomException。以下是提供的OpenAPI定义文件和现有Maven插件配置片段:

OpenAPI定义文件(open-ui.yaml)

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Test Api
  license:
    name: MIT
servers:
  - url: "https://{domain}/test/{basePath}"

paths:
  /users:
    get:
      summary: users
      operationId: getUsers
      tags:
        - users
      parameters:
        - name: limit
          in: query
          schema:
            type: integer

      responses:
        '200':
          description: A page of users
          content:
            application/v1+json; charset=utf-8:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/UserDto'

现有Maven插件配置(pom.xml片段)

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>5.3.1</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>
                    ${project.basedir}/swagger/open-ui.yaml
                </inputSpec>
                <!-- 省略其他配置 -->
            </configuration>
        </execution>
    </executions>
</plugin>
解决方案

要让生成的REST接口方法签名统一抛出CustomException,可通过以下两种方式配置插件:

方式一:插件参数配置(推荐)

无需修改模板,直接通过插件参数实现需求:

  1. 需确保CustomException已在项目中定义,且为受检异常(继承自java.lang.Exception)
  2. 更新pom.xml中的插件配置:
<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>5.3.1</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/swagger/open-ui.yaml</inputSpec>
                <!-- 指定生成Spring风格代码 -->
                <generatorName>spring</generatorName>
                <configOptions>
                    <!-- 仅生成接口类,不生成实现类 -->
                    <interfaceOnly>true</interfaceOnly>
                    <!-- 指定全局抛出的异常类全限定名 -->
                    <exception>com.yourpackage.CustomException</exception>
                    <!-- 强制所有接口方法抛出异常 -->
                    <throwExceptionIfNotFound>true</throwExceptionIfNotFound>
                </configOptions>
                <!-- 配置导入映射,确保异常类被正确导入到生成的接口中 -->
                <importMappings>
                    <importMapping>CustomException=com.yourpackage.CustomException</importMapping>
                </importMappings>
            </configuration>
        </execution>
    </executions>
</plugin>

方式二:自定义模板

若参数配置无法满足个性化需求,可通过修改生成器模板实现:

  1. 获取Spring生成器的api.mustache模板文件(该模板用于生成REST接口类)
  2. 修改模板中方法签名的末尾,添加 throws CustomException
  3. 在插件配置中指定自定义模板的目录路径:
<configuration>
    <!-- 其他已有配置 -->
    <templateDirectory>${project.basedir}/src/main/resources/openapi-templates</templateDirectory>
</configuration>

注意:将com.yourpackage替换为项目中CustomException实际所在的包路径。配置完成后执行mvn clean install,生成的接口方法签名将自动包含throws CustomException。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 20:15:02