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

如何阻止jsonschema2pojo动态为父类MyBaseClass添加属性?

阻止jsonschema2pojo修改已有父类的方案

要让MyBaseClass保持为空的符号类,避免jsonschema2pojo动态为其添加属性和方法,可通过以下两种方式实现:

1. 在JSON Schema中标记父类为已存在类

修改你的JSON Schema,在extends节点添加"existingClass": true,明确告知工具该父类是已存在的,无需生成或修改:

{
  "JavaType":"com.a.b.c.MyClass",
  "extends": {
    "type": "object",
    "javaType": "com.a.b.MyBaseClass",
    "existingClass": true
  }
}

2. 配置构建插件优先使用现有类

在Maven或Gradle的jsonschema2pojo插件配置中,开启useExistingClasses选项,让工具全局优先使用类路径中已存在的类,而非重新生成:

Maven插件配置示例

<plugin>
  <groupId>org.jsonschema2pojo</groupId>
  <artifactId>jsonschema2pojo-maven-plugin</artifactId>
  <version>1.2.1</version>
  <configuration>
    <sourceDirectory>${project.basedir}/src/main/resources/schema</sourceDirectory>
    <targetPackage>com.a.b.c</targetPackage>
    <!-- 开启优先使用现有类的规则 -->
    <useExistingClasses>true</useExistingClasses>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
    </execution>
  </executions>
</plugin>

前置条件

需提前手动创建空的com.a.b.MyBaseClass类,并确保它处于项目的类路径中,这样jsonschema2pojo在构建时会直接引用该类,不会自动生成或修改它。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 07:55:11