[TOC]
# **1. 启用注解**
`@GoEnableFastJson`
`@GoDesensitized`
# **2. 配置说明**
```
FastBoot:
fastjson:
supported-media-types: # 支持媒体类型,默认text/html、application/json
- text/html;charset=UTF-8
- application/json;charset=UTF-8
config: # 配置
charset: utf-8 # 编码格式,默认UTF-8
date-format: yyyy-MM-dd HH:mm:ss # 日期格式,默认yyyy-MM-dd HH:mm:ss
serializer-features: # 序列化
- PrettyFormat # 格式化输出
- WriteNullBooleanAsFalse # 布尔类型如果为null,输出为false,而不是null
- WriteEnumUsingToString # 枚举类型用ToString输出
...
features:
- OrderedField
...
...
desensitize: true # 启用脱敏,默认true
```
# **3. 注意事项**
> Sprinboot中默认使用Jackson提供Json支持,这里整合了FastJson
> 如需使用,需开启@GoEnableFastJson
> 如需使用脱敏,字段启用 @GoDesensitized(type = xxx)即可,类型如下
| 类型 | 描述 |
| --- | --- |
| USER_ID | 用户id |
| CHINESE_NAME | 中文名 |
| ID_CARD | 身份证|
| FIXED_PHONE | 座机号 |
| MOBILE_PHONE | 手机号 |
| ADDRESS | 地址 |
| EMAIL | 邮件 |
| PASSWORD | 车牌 |
| BANK_CARD | 银行卡 |
# **4. 示例说明**
MockUserDesensitized.java,与下文中MockUser.java结果一致
```
@Data
public class MockUserDesensitized {
@GoDesensitized(type = DesensitizedType.CHINESE_NAME)
@JSONField(ordinal = 1)
private String name;
@JSONField(ordinal = 2)
private Date birthDay;
@JSONField(ordinal = 3, format = "yyyy-MM-dd")
private Date registerDay;
@JSONField(ordinal = 4)
@GoDesensitized(type = DesensitizedType.ID_CARD)
private String idCard;
@JSONField(ordinal = 5)
@GoDesensitized(type = DesensitizedType.FIXED_PHONE)
private String phone;
@JSONField(ordinal = 6)
@GoDesensitized(type = DesensitizedType.MOBILE_PHONE)
private String tel;
@JSONField(ordinal = 7)
@GoDesensitized(type = DesensitizedType.ADDRESS)
private String address;
@JSONField(ordinal = 8)
@GoDesensitized(type = DesensitizedType.EMAIL)
private String email;
@JSONField(ordinal = 9)
@GoDesensitized(type = DesensitizedType.PASSWORD)
private String password;
@JSONField(ordinal = 10)
@GoDesensitized(type = DesensitizedType.CAR_LICENSE)
private String carnumber;
@JSONField(ordinal = 11)
private StatusEnum status;
@JSONField(ordinal = 12, format = "0.00")
private BigDecimal balance;
@JSONField(ordinal = 13)
private Boolean enable;
}
```
MockData.java
```
public class MockData {
// 用户信息
public static MockUser user() {
MockUser user = new MockUser()//
.setName("张三")//
.setBirthDay(DateTime.now())//
.setRegisterDay(DateTime.now())//
.setIdCard("51343620000320711X")//
.setPhone("09127518479")//
.setTel("13800138000")//
.setAddress("xx市xx区xxxx街道xxx号")//
.setEmail("123456789@qq.com")//
.setPassword(RandomUtil.randomString(8))//
.setCarnumber("京A88888")//
.setStatus(StatusEnum.A)//
.setBalance(new BigDecimal(100.123));//
return user;
}
public static MockUserDesensitized user2() {
MockUser user = user();
MockUserDesensitized user2 = new MockUserDesensitized();
BeanUtil.copyProperties(user, user2, true);
return user2;
}
```
## **4.1 明文数据**
FastJsonController.java
```
@RequestMapping("/json")
public MockUser json() {
return MockData.user();
}
```
```
{
"code": 0,
"msg": "操作成功",
"data": {
"name": "张三",
"birthDay": "2021-06-27 00:05:22",
"registerDay": "2021-06-27 00:05:22",
"idCard": "51343620000320711X",
"phone": "09127518479",
"tel": "13800138000",
"address": "xx市xx区xxxx街道xxx号",
"email": "123456789@qq.com",
"password": "jox3uji2",
"carnumber": "京A88888",
"status": "A",
"balance": "100.12",
"enable": false
},
"status": true
}
```
## **4.2 脱敏数据**
FastJsonController.java
```
@RequestMapping("/json/desensitized")
public MockUserDesensitized desensitized() {
return MockData.userDesensitized();
}
```
```
{
"code": 0,
"msg": "操作成功",
"data": {
"name": "张*",
"birthDay": "2021-06-27 00:05:50",
"registerDay": "2021-06-27",
"idCard": "5***************1X",
"phone": "0912*****79",
"tel": "138****8000",
"address": "xx市xx区xx********",
"email": "1********@qq.com",
"password": "********",
"carnumber": "京A8***8",
"status": "A",
"balance": "100.12",
"enable": false
},
"status": true
}
```
> [`常用方法使用指南`](https://www.cnblogs.com/wbxk/p/10064737.html)