simple-starter-dict
# 简介
提供注解简化使用:字典翻译
字典服务可扩展自定义实现使用
服务类返回参数支持单体字段、列表、实体嵌套
# 引入依赖
<dependencies>
<dependency>
<groupId>cn.iosd</groupId>
<artifactId>simple-starter-dict</artifactId>
<version>Version</version>
</dependency>
</dependencies>
1
2
3
4
5
6
7
2
3
4
5
6
7
# 配置项
simple:
dict:
##字典 缺省项为true
enabled: true
1
2
3
4
2
3
4
# 功能项
# 提供开放字典接口
仅需实现DictService即可
public interface DictService {
/**
* 根据参数获取对应字典列表
*
* @param dictionaryParams 获取对应字典列表所需的参数
* @return 字典列表
*/
List<DictItem> getDictItemList(String dictionaryParams);
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 注解字段说明
示例
@Schema(description = "身份标识-自定义远程调用接口实现类字典")
@DictField(dictionaryParams = "idCard", dictImplClass = CustomDictServiceImpl.class, relatedField = "idCardText")
private Integer idCard;
private String idCardText;
1
2
3
4
5
2
3
4
5
dictImplClass: 自定义实现DictService接口的类(注:若仅有一个字典实现类可不用填写。)
relatedField: 需要将字典翻译后的对象赋值到的关联字段
dictionaryParams: 字典查询所需参数,在字典实现类中根据此参数查询出对于的字典列表。后续会将idCard值与字典列表对应并获取出字典翻译值。
# 以下为实现字典接口示例
# 本地文件json字典翻译
从本地文件读取字典项列表
# 实现代码逻辑
/**
* 从本地文件读取字典项列表
*
*/
@Service
public class LocalDictServiceImpl implements DictService {
/**
* 本地字典文件目录
*/
@Value("${simple.dict.resourceDictFileDir:/dict.json}")
private String resourceDictFileDir;
/**
* 用于反序列化字典项的类型引用
*/
public static final TypeReference<Map<String, List<DictItem>>> TYPE_DICT_ITEM = new TypeReference<Map<String, List<DictItem>>>() {
};
@Override
public List<DictItem> getDictItemList(String dictionaryParams) {
try (InputStream inputStream = this.getClass().getResourceAsStream(resourceDictFileDir)) {
ObjectMapper mapper = new ObjectMapper();
Map<String, List<DictItem>> dictItemList = mapper.readValue(inputStream, TYPE_DICT_ITEM);
return dictItemList.get(dictionaryParams);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 文件
resources/dict.json
{"leader":[{"value":"1","label":"领导"},{"value":"2","label":"苦工"}],"sex":[{"value":"1","label":"女"},{"value":"2","label":"男"}]}
1
# 示例1
@Schema(description = "性别-json文件调用字典")
@DictField(dictionaryParams = "sex", dictImplClass = LocalDictServiceImpl.class, relatedField = "sexText")
private Integer sex;
private String sexText;
1
2
3
4
5
2
3
4
5
# 示例2
@Schema(description = "性别-json文件调用字典")
@DictField(dictionaryParams = "sex", relatedField = "sexText")
private Integer sex;
private String sexText;
1
2
3
4
5
2
3
4
5
# 远程接口字典翻译
调用外部接口读取字典项列表
# 实现代码逻辑
/**
* 调用远程接口获取字典项列表
*
*/
@Service
public class RemoteDictServiceImpl implements DictService {
private RestTemplate restTemplate = new RestTemplate();
@Value("${simple.dict.remoteBaseUrl:}")
private String remoteBaseUrl;
/**
* 用于反序列化字典项的类型引用
*/
public static final ParameterizedTypeReference<List<DictItem>> TYPE_DICT_ITEM = new ParameterizedTypeReference<List<DictItem>>() {
};
@Override
public List<DictItem> getDictItemList(String dictionaryParams) {
String path = remoteBaseUrl + dictionaryParams;
ResponseEntity<List<DictItem>> responseEntity = restTemplate.exchange(path, HttpMethod.GET, null, TYPE_DICT_ITEM);
return responseEntity.getBody();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 示例1
@Schema(description = "隐藏身份-默认远程调用接口实现类字典")
@DictField(dictionaryParams = "http://127.0.0.1:11120/dict/remote/hideIdentity", dictImplClass = RemoteDictServiceImpl.class , relatedField = "hideIdentityText")
private Integer hideIdentity;
private String hideIdentityText;
1
2
3
4
5
2
3
4
5
# 示例2
配置文件:
simple:
dict:
remoteBaseUrl: http://127.0.0.1:11120
1
2
3
2
3
使用:
@Schema(description = "隐藏身份-默认远程调用接口实现类字典")
@DictField(dictionaryParams = "/dict/remote/hideIdentity", dictImplClass = RemoteDictServiceImpl.class, relatedField = "hideIdentityText")
private Integer hideIdentity;
private String hideIdentityText;
1
2
3
4
5
2
3
4
5
上次更新: 2024/03/25, 02:28:08