把es的properties粘贴进一个json文件就可以执行
加入依赖
com.fasterxml.jackson.core
jackson-databind
2.12.4
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
public class VoGenerator {
public static void main(String[] args) throws IOException {
// 指定JSON文件路径
String jsonFilePath = "D:\\software\\a.json";
// 创建ObjectMapper对象
ObjectMapper objectMapper = new ObjectMapper();
// 读取JSON文件
JsonNode jsonNode = objectMapper.readTree(new File(jsonFilePath));
// 创建VO类字符串
String voClass = generateVoClass("Vo", jsonNode);
// 打印生成的VO类
System.out.println(voClass);
}
private static String generateVoClass(String className, JsonNode jsonNode) {
// 创建一个StringBuilder来构建VO类
StringBuilder voClassBuilder = new StringBuilder();
// 开始生成VO类
voClassBuilder.append("public class ").append(capitalize(className)).append(" {\n");
// 获取JSON对象的字段迭代器
Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> entry = fields.next();
String fieldName = entry.getKey();
JsonNode fieldNode = entry.getValue();
if (fieldNode.has("type")) {
String fieldType = getJavaFieldType(fieldNode);
// 如果字段类型是"nested",则生成嵌套的VO类列表
if ("nested".equals(fieldType)) {
String nestedClassName = capitalize(fieldNameToCamelCase(fieldName)) + "Vo";
String nestedVoClass = generateVoClass(nestedClassName, fieldNode.get("properties"));
voClassBuilder.append(" @JsonProperty(\"").append(fieldName).append("\")\n");
voClassBuilder.append(" private List<").append(nestedClassName).append("> ").append(fieldNameToCamelCase(fieldName)).append(";\n");
voClassBuilder.append(nestedVoClass);
} else {
// 生成VO类字段
voClassBuilder.append(" @JsonProperty(\"").append(fieldName).append("\")\n");
voClassBuilder.append(" private ").append(fieldType).append(" ").append(fieldNameToCamelCase(fieldName)).append(";\n");
}
}
}
// 结束VO类
voClassBuilder.append("}\n");
return voClassBuilder.toString();
}
private static String getJavaFieldType(JsonNode fieldNode) {
// 根据JSON节点的类型来确定Java字段类型
String fieldType;
if (fieldNode.has("type")) {
String type = fieldNode.get("type").asText();
switch (type) {
case "integer":
fieldType = "Integer";
break;
case "float":
fieldType = "Float";
break;
case "date":
fieldType = "Date"; // 需要导入java.util.Date类
break;
case "nested":
fieldType = "nested";
break;
default:
fieldType = "String";
break;
}
} else {
fieldType = "String"; // 默认为String类型
}
return fieldType;
}
private static String fieldNameToCamelCase(String fieldName) {
// 将下划线分隔的字段名转换为驼峰命名
String[] parts = fieldName.split("_");
StringBuilder camelCaseName = new StringBuilder();
for (int i = 0; i < parts.length; i++) {
String part = parts[i];
if (i == 0) {
camelCaseName.append(part);
} else {
camelCaseName.append(part.substring(0, 1).toUpperCase()).append(part.substring(1));
}
}
return camelCaseName.toString();
}
private static String capitalize(String str) {
// 将字符串的首字母大写
if (str == null || str.isEmpty()) {
return str;
}
return Character.toUpperCase(str.charAt(0)) + str.substring(1);
}
}