如何从JSON Schema生成自定义属性名的POJO(支持Maven插件)
我有如下YAML Schema定义:
"$schema": "https://json-schema.org/draft/2020-12/schema" "title": "Book" type: object properties: f1: type: string f2: type: integer
我希望生成这样的Java POJO:
public class Book{ @JsonProperty("f1") private String title; @JsonProperty("f2") private Integer length; //getters, setters etc. }
核心需求是:Schema里的字段名(f1、f2)可读性差,生成POJO时要把字段名改成可读的(title、length),同时保留@JsonProperty注解指向原字段名。
我试过用jsonschema2pojo的Maven插件,没找到直接实现名称转换的方法。之前尝试在Schema里加description或自定义属性来指定新名称,再配置自定义注解器修改字段名,但生成的getter、setter和构建器还是用原有的不可读名称。
请问有什么解决办法?可以替换其他工具,但必须能在Maven构建阶段从JSON/YAML Schema生成Java代码。
方法1:给jsonschema2pojo配置自定义命名策略
jsonschema2pojo支持自定义字段命名策略,你可以通过实现PropertyNamingStrategy来映射字段名:
- 编写自定义命名策略类
public class CustomPropertyNamingStrategy extends DefaultPropertyNamingStrategy { private static final Map<String, String> NAME_MAP = Map.of( "f1", "title", "f2", "length" ); @Override public String translatePropertyName(String propertyName) { return NAME_MAP.getOrDefault(propertyName, super.translatePropertyName(propertyName)); } }
- 在Maven插件中配置该策略
<plugin> <groupId>org.jsonschema2pojo</groupId> <artifactId>jsonschema2pojo-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <sourceDirectory>${project.basedir}/src/main/resources/schemas</sourceDirectory> <targetPackage>com.yourpackage.model</targetPackage> <!-- 指定自定义命名策略类 --> <propertyNamingStrategy>com.yourpackage.CustomPropertyNamingStrategy</propertyNamingStrategy> <useJackson2>true</useJackson2> <includeHashcodeAndEquals>true</includeHashcodeAndEquals> <includeToString>true</includeToString> </configuration> </plugin>
这样生成的字段、getter/setter都会用映射后的可读名称,@JsonProperty自动保留原Schema字段名。
方法2:用jsonschema2pojo的扩展属性直接指定字段名
jsonschema2pojo支持x-jsonschema2pojo-property扩展属性,可以直接在Schema里定义生成后的Java字段名:
修改你的YAML Schema:
"$schema": "https://json-schema.org/draft/2020-12/schema" "title": "Book" type: object properties: f1: type: string x-jsonschema2pojo-property: title f2: type: integer x-jsonschema2pojo-property: length
Maven插件配置中确保开启扩展支持(默认已开启,可显式配置):
<configuration> <!-- 其他配置 --> <enableExtensions>true</enableExtensions> </configuration>
这种方式最直接,生成的POJO字段名、getter/setter都会用你指定的名称,@JsonProperty自动绑定原字段名,完全符合需求。
方法3:替换为OpenAPI Generator
如果jsonschema2pojo不符合预期,可以用OpenAPI Generator,它对Schema的字段映射支持更灵活:
- 转换为OpenAPI规范(或直接用OpenAPI定义Schema)
openapi: 3.0.0 info: title: Book Schema version: 1.0.0 components: schemas: Book: type: object properties: f1: type: string x-codegen-property-name: title f2: type: integer x-codegen-property-name: length
- 配置Maven插件
<plugin> <groupId>org.openapitools</groupId> <artifactId>openapi-generator-maven-plugin</artifactId> <version>6.6.0</version> <executions> <execution> <goals> <goal>generate</goal> </goals> <configuration> <inputSpec>${project.basedir}/src/main/resources/book-openapi.yaml</inputSpec> <generatorName>java</generatorName> <configOptions> <library>jackson</library> <useJackson>true</useJackson> <dateLibrary>java8</dateLibrary> </configOptions> <modelPackage>com.yourpackage.model</modelPackage> <generateModels>true</generateModels> </configuration> </execution> </executions> </plugin>
通过x-codegen-property-name指定Java字段名,生成的代码会自动添加@JsonProperty注解,getter/setter也使用新名称。
内容的提问来源于stack exchange,提问作者Loginus

