Using ChatGPT to Build a Simple Java Enum Mapping Framework
This article demonstrates how Java developers can leverage ChatGPT to design and iteratively refine a lightweight backend framework that automatically scans annotated enums and generates key‑description maps for front‑end consumption, complete with code examples, performance testing, and deployment guidance.
Many Java developers face repetitive work when exposing enum descriptions to the front‑end; this guide shows how to use ChatGPT to create a small Java framework that automates the generation of a map of enum keys and their descriptions.
1. Clarify the problem and features – Define the goal of automatically providing a map of enum key and desc values, and design an annotation @EnumInfo with three fields: name, key, and desc.
2. Interact with ChatGPT – Prompt ChatGPT with the problem statement; it returns an initial demo framework and explains the purpose of each field, allowing developers to quickly obtain skeleton code.
3. Deep interaction and optimization – Ask ChatGPT for code improvements and exception‑handling suggestions. The article shows a before‑and‑after comparison (images in the original) and presents the optimized version of the EnumInfo annotation.
4. Incremental improvement – Through repeated dialogue, the core classes of the framework emerge, including PackageScanner, PropertiesUtils, EnumInfo, EnumContext, EnumContextFactory, EnumDefinition, EnumDefinitionRegistry, and DefaultEnumFactory. A high‑level architecture diagram (original image) illustrates their relationships.
5. Framework usage test – The article contrasts a manual map‑initialization approach with the annotation‑driven solution. Sample code for manual initialization is:
public HashMap<String, Map<Integer, String>> initEnumMap() {
enumMap = new HashMap<>();
enumMap.put("前端获取枚举map的key", XXXEnum.getEnumMap());
enumMap.put("前端获取枚举map的key", XXXEnum.getEnumMap());
enumMap.put("前端获取枚举map的key", XXXEnum.getEnumMap());
// ...
return enumMap;
}A REST endpoint that retrieves the enum map is:
@RequestMapping("/getEnum")
public Result<Map<String, Map<String, String>>> getEnum(String enumKey) {
try {
EnumContext enumContext = EnumContextFactory.getEnumContext();
newEnumMap = enumContext.getEnumIndexMap();
newEnumMap.put(BUID.getKey(), getBuIdMap());
} catch (Exception e) {
log.error("获取枚举map出错!enumKey:{}", enumKey, e);
return Result.createFail(e.getMessage());
}
if (StringUtils.isBlank(enumKey)) {
return Result.createWithSuc(newEnumMap);
}
Map<String, Map<String, String>> resultMap = new HashMap<>();
resultMap.put(enumKey, newEnumMap.get(enumKey));
return Result.createWithSuc(resultMap);
}An example of an annotated enum using the framework is:
@EnumInfo(name = "StatusEnum", key = "code", desc = "description")
public enum StatusEnum {
SUCCESS(200, "Success"),
ERROR(500, "Error");
private final int code;
private final String description;
StatusEnum(int code, String description) {
this.code = code;
this.description = description;
}
public int getCode() { return code; }
public String getDescription() { return description; }
}After adding the annotation, developers only need to declare the enum; the framework automatically registers it and makes its key‑description map available to the front‑end without further code changes, dramatically reducing integration and regression effort.
6. Performance testing – Once deployed, the framework was stress‑tested on a 4C‑4G machine, achieving up to 1000 QPS with CPU usage around 30 % and stable memory consumption. Standard tools such as LoadRunner or Apache JMeter can be used for further benchmarking.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JD Retail Technology
Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
