gpt4 book ai didi

java - jsonschema2pojo 还生成自定义注释器的对象

转载 作者:太空宇宙 更新时间:2023-11-04 09:28:58 26 4
gpt4 key购买 nike

我正在使用 jsonschema2pojo maven 插件生成 pojo。使用自定义注释器,我添加了一些 lombok 注释(效果很好),但它也为注释创建了变量。有没有办法强制 jsonschema2pojo 不创建用于注释的对象变量而只拥有变量?

json 架构

{
"title": "Person",
"type": "object",
"properties": {
"lombok-builder": true,
"lombok-data": true,
"lombok-to-string": true,
"name": {
"type": "string"
}
}
}

自定义注释器

 @Override
public void propertyField(JFieldVar field, JDefinedClass clazz, String property, JsonNode propertyNode) {
super.propertyField(field, clazz, property, propertyNode);

if (property.equals("lombok-builder")) {
clazz.annotate(Builder.class);
} else if (property.equals("lombok-data")) {
clazz.annotate(Data.class);
} else if (property.equals("lombok-to-string")) {
clazz.annotate(ToString.class);
}
}

由jsonschema2pojo生成的Person.java


package com.package;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;


/**
* Person
* <p>
*
*
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@Builder
@Getter
@Setter
@Data
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@ToString
@JsonPropertyOrder({
"lombok-builder",
"lombok-getter",
"lombok-setter",
"lombok-data",
"lombok-equals-and-hash-code",
"lombok-no-args-constructor",
"lombok-all-args-constructor",
"lombok-to-string",
"name"
})
public class Person {

@JsonProperty("lombok-builder")
public Object lombokBuilder;
@JsonProperty("lombok-data")
public Object lombokData;
@JsonProperty("lombok-to-string")
public Object lombokToString;
@JsonProperty("name")
public String name;

}

如您所见,注释工作正常,但我得到了 public Object lombokToString;@JsonProperty("lombok-to-string") 等:|

最佳答案

经过一段时间的研究,我意识到我的做法是错误的。需要使用 additionalProperties 处理 propertyInclusion以下是工作注释器和示例架构,供有类似问题的任何人使用。

import com.sun.codemodel.JDefinedClass;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.*;
import org.jsonschema2pojo.AbstractAnnotator;

public class LombokAnnotator extends AbstractAnnotator {

@Override
public void propertyInclusion(JDefinedClass clazz, JsonNode schema) {

JsonNode additionalProperties = schema.get("additionalProperties");

try {
additionalProperties.fieldNames().forEachRemaining(property -> {
Class annotation = getAnnotation(property);
if (!annotation.equals(IllegalArgumentException.class)) {
clazz.annotate(annotation);
}
});
} catch (NullPointerException e) {
System.out.println(String.format("No additionalProperties defined for %s.", clazz.fullName()));
}

}

@Override
public boolean isAdditionalPropertiesSupported() {
return false;
}

和更新的 json 架构

{
"title": "Person",
"type": "object",
"additionalProperties": {
"lombok-builder": true,
"lombok-data": true,
"lombok-getter": true,
"lombok-setter": true,
"lombok-equals-and-hash-code": true,
"lombok-no-args-constructor": true,
"lombok-all-args-constructor": true,
"lombok-to-string": true
},
"properties": {
"name": {
"type": "string"
}
}
}

关于java - jsonschema2pojo 还生成自定义注释器的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57351685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com