{"id":13597531,"url":"https://github.com/houkunlin-starter/system-dict-starter","last_synced_at":"2026-01-12T14:02:00.285Z","repository":{"id":57725012,"uuid":"360566791","full_name":"houkunlin-starter/system-dict-starter","owner":"houkunlin-starter","description":"系统数据字典自动翻译成字典文本。可集合系统数据库中存储的用户数据字典，也可使用枚举做系统数据字典，主要用在返回数据给前端时自动把字典值翻译成字典文本信息。","archived":false,"fork":false,"pushed_at":"2025-09-12T18:03:14.000Z","size":733,"stargazers_count":29,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-12T20:49:44.854Z","etag":null,"topics":["java"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/houkunlin-starter.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2021-04-22T15:27:52.000Z","updated_at":"2025-09-12T18:03:18.000Z","dependencies_parsed_at":"2024-01-14T04:41:27.513Z","dependency_job_id":"5841ddf8-5f51-42f3-8c10-664828173f48","html_url":"https://github.com/houkunlin-starter/system-dict-starter","commit_stats":null,"previous_names":["houkunlin/system-dic-starter"],"tags_count":38,"template":false,"template_full_name":null,"purl":"pkg:github/houkunlin-starter/system-dict-starter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houkunlin-starter%2Fsystem-dict-starter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houkunlin-starter%2Fsystem-dict-starter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houkunlin-starter%2Fsystem-dict-starter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houkunlin-starter%2Fsystem-dict-starter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/houkunlin-starter","download_url":"https://codeload.github.com/houkunlin-starter/system-dict-starter/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houkunlin-starter%2Fsystem-dict-starter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28340223,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T12:22:26.515Z","status":"ssl_error","status_checked_at":"2026-01-12T12:22:10.856Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java"],"created_at":"2024-08-01T17:00:35.404Z","updated_at":"2026-01-12T14:02:00.273Z","avatar_url":"https://github.com/houkunlin-starter.png","language":"Java","funding_links":[],"categories":["Java"],"sub_categories":[],"readme":"[![Maven Central](https://img.shields.io/maven-central/v/com.houkunlin/system-dict-starter.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.houkunlin%22%20AND%20a:%22system-dict-starter%22)\n[![Java CI with Gradle](https://github.com/houkunlin-starter/system-dict-starter/actions/workflows/gradle.yml/badge.svg)](https://github.com/houkunlin-starter/system-dict-starter/actions/workflows/gradle.yml)\n\n# 系统字典 Starter\n\n在日常项目开发中，不免都会用到一些数据字典的信息，以及前端展示的时候通常也需要把这些数据字典值转换成具体字典文本信息。遇到这种场景通常都是后端把字典的文本转换好一起返回给前端，前端只需要直接展示即可。\n\n一般情况下后端可能需要单独给返回对象创建一个字段来存储对应的字典文本值，然后进行手动的处理，这种方式通常比较繁琐，在字段多的时候会增加更多的工作量。\n\n本项目基于 Jackson 的自定义注解功能实现了这一自动转换过程，不需要在对象中定义存放字典文本的字段，只需要在字段上使用特定的注解配置，Jackson序列化的时候即可自动把字典值转换成字典文本。\n\n**本项目只适用使用 Jackson 做 JSON 序列化，在 fastjson 下失效**\n\n**`v1.5.8` 将成为最后一个基于 Java 8 字节码发布的版本（基于 Spring Boot 2.7）**\n\n| 版本           | 基于 SpringBoot 版本 | 测试兼容 SpringBoot 版本 |\n|--------------|------------------|--------------------|\n| `v1.5.8` 及以下 | `2.7.18`         | `3.4.5`            |\n| `v1.6.0` 及以上 | `3.4.6`          | `3.4.6`            |\n\n**Maven**\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.houkunlin\u003c/groupId\u003e\n    \u003cartifactId\u003esystem-dict-starter\u003c/artifactId\u003e\n    \u003cversion\u003e${latest.version}\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n**Gradle**\n\n```groovy\nimplementation \"com.houkunlin:system-dict-starter:${latest.release}\"\n```\n\n\n\n#### 详细使用文档请点击查看 [基础用法文档](./usage.md) \n#### 系统更新日志 [系统更新日志](./changelog.md) \n\n## 如何启用？\n\n- 在应用启动类上添加 `SystemDictScan` 注解\n- 后续步骤请看本文后面内容\n\n```java\n// 启动类上加注解\n@SystemDictScan\npublic class Application {\n    public static void main(String[] args) {\n        SpringApplication.run(Application.class);\n    }\n}\n```\n\n**从 `v1.4.11` 到 `v1.5.0` 版本产生了一些破坏性变更，请谨慎升级。移除了 DictText.Type 改为使用 DictBoolType；修改了 Redis 存储字典文本的方式，改为 Redis Hash 存储字典文本内容。**\n\n\n\n## 使用枚举对象做系统字典\n\n- 需要实现 `DictEnum` 接口的枚举对象才能被扫描到\n- 使用 `DictType` 注解应用到枚举上自定义字典类型名称和说明\n\n\u003e `@DictConverter` 一般与 `@DictType` 配合使用。\n\u003e\n\u003e `@DictType` 用来标记枚举对象的字典类型代码\n\u003e\n\u003e `@DictConverter` 用来标记是否对这个枚举对象生成 `org.springframework.core.convert.converter.Converter` 转换对象，提供使用枚举接收参数时自动转换字典值到相应枚举对象类型的功能\n\n```java\n@DictConverter\n@DictType(value = \"PeopleType\", comment = \"用户类型\")\n@Getter\n@AllArgsConstructor\npublic enum PeopleType implements DictEnum\u003cInteger\u003e {\n    /** 系统管理员 */\n    ADMIN(0, \"系统管理\"),\n    /** 普通用户 */\n    USER(1, \"普通用户\"),\n    ;\n    private final Integer value;\n    private final String title;\n\n    /**\n     * Jackson 枚举处理，把枚举值转换成枚举对象\n     *\n     * @param code 代码\n     * @return 枚举对象\n     */\n    @JsonCreator\n    public static PeopleType getItem(Integer code) {\n        return DictEnum.valueOf(values(), code);\n    }\n}\n```\n\n\n\n## 字典文本自动转换\n\n- 在字段中使用 `DictText` 注解\n\n```java\n@Data\n@AllArgsConstructor\nclass Bean {\n    @DictText(\"PeopleType\")\n    private String userType1 = \"1\";\n\n    @DictText(value = \"PeopleType\", array = @Array)\n    private String userType2 = \"1,2,3\";\n\n    @DictText(value = \"PeopleType\", array = @Array(toText = false))\n    private List\u003cString\u003e userType3 = Arrays.asList(\"1\", \"2\", \"3\");\n}\n```\n\n\n\n## 提供一些其他字典信息到系统字典存储对象中\n\n- 实现 `DictProvider` 接口并扫描到SpringBoot中\n\n```java\n@Component\npublic class MyProvider implements DictProvider {\n    @Override\n    public boolean isStoreDictType() {\n        return true;\n    }\n\n    @Override\n    public Iterator\u003cDictTypeVo\u003e dictTypeIterator() {\n        // 从其他地方（其他服务、数据库、本地文件）加载完整的数据字典信息（字典类型+字典值列表）\n        // 从这里返回的数据字典信息将会被存入缓存中，以便下次直接调用，当有数据变动时可以发起 RefreshDictEvent 事件通知更新字典信息\n        final DictTypeVo typeVo = DictTypeVo.newBuilder(\"name\", \"测试字典\")\n            .add(\"1\", \"测试1\")\n            .add(\"2\", \"测试2\")\n            .build();\n        return Collections.singletonList(typeVo).iterator();\n    }\n}\n```\n\n\n\n## 当在系统字典中获取不到数据时，请求第三方服务获取字典信息\n\n- 实现 `RemoteDict` 接口并扫描到SpringBoot中，当自行定义 `LocalDictStore` 对象时，此时的默认`RemoteDict`无法生效，需要手动处理此类情况。\n- 例如无法从 `DictStore` 获取到字典信息时，可以使用 `RemoteDict` 从特定的系统服务中获取字典信息\n\n```java\n@Component\npublic class MyRemoteDict implements RemoteDict {\n    @Override\n    public DictTypeVo getDictType(final String type) {\n        // 从其他地方（其他服务、数据库、本地文件）加载一个完整的数据字典信息（字典类型+字典值列表）\n        return null;\n    }\n\n    @Override\n    public String getDictText(final String type, final String value) {\n        // 从其他地方（其他服务、数据库、本地文件）加载一个字典文本信息\n        return null;\n    }\n}\n```\n\n\n\n## 全局工具类直接获取字典信息\n\n- 调用 `DictUtil` 对象\n\n```java\n@Component\n@AllArgsConstructor\npublic class CommandRunnerTests implements CommandLineRunner {\n    @Override\n    public void run(final String... args) throws Exception {\n        System.out.println(DictUtil.getDictText(\"PeopleType\", \"1\"))\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoukunlin-starter%2Fsystem-dict-starter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhoukunlin-starter%2Fsystem-dict-starter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoukunlin-starter%2Fsystem-dict-starter/lists"}