GsonFormat背景
在我们日常打码的过程中,免不了会定义非常多的bean,少则几个字段,多则几十个,还要避免不出现错误,手敲bean字段是一件吃力不讨好的事情,那么有什么工具可以帮我们解决这个问题么?大佬已经帮我们想到了,请用插件GsonFormat
GsonFormat用法
插件下载(idea为例)
File -> Setttings -> plugins 检索GsonFormat -> install ->重启idea->插件安装完成
插件使用
测试json
{
"error_code": 0,
"reason": "",
"result": {
"data": [
{
"content": "窗前明月光 疑是地上霜",
"hashId": "903f01b8-695d-43c1-8f12-a12bf4ce5925",
"unixtime": 1663726422847,
"updatetime": "2022-09-21 10:12"
},
{
"content": "举头望明月 低头思故乡",
"hashId": "8fbbdcfa-81be-4bbc-aa31-d0b641ee5749",
"unixtime": 1663726422847,
"updatetime": "2022-09-21 10:13"
}
]
}
}
新建一个测试类
右键-> Generate -> GsonFormatPlus
放入json
点击ok 可勾选字段 自动生成bean字段
@NoArgsConstructor
@Data
public class DataDto {
@JSONField(name = "error_code")
private Integer errorCode;
@JSONField(name = "reason")
private String reason;
@JSONField(name = "result")
private ResultDTO result;
@NoArgsConstructor
@Data
public static class ResultDTO {
@JSONField(name = "data")
private List<DataDTO> data;
@NoArgsConstructor
@Data
public static class DataDTO {
@JSONField(name = "content")
private String content;
@JSONField(name = "hashId")
private String hashId;
@JSONField(name = "unixtime")
private Long unixtime;
@JSONField(name = "updatetime")
private String updatetime;
}
}
}
总结
使用gsonformat告别手写bean,提高准确率,工作效率。增加摸鱼时间