今天给大家介绍一款很实用的GsonFormat插件,GsonFormat插件能够将我们的json格式数据生成对应的实体类,层级关系通过内部类来嵌套,这个工具有时候是很实用的,例如以前公司在对接第三方接口的时候,他们通常提供的入参形式是json形式,但是对于java来说,我们为了方便赋值和接收三方的值,要将其转成对象,所以这个时候我们就需要创建一个对象,但是如果json参数比较多,很容易出错,这个工具就用到了,完美解决我们的机械操作问题。下面来介绍如何安装以及如何使用。
安装插件
和安装其他插件方式相同,Mac在Preferences ->》plugins
输入GsonFormat,如下图所示,点击Install 下载后点击OK,会提示重启,点击重新启动则可。
使用方式
先构造一个正确JSON格式数据,如下
{
"acIdttbPressFilterConfEntities": [],
"acIdttbPressNodeConfEntityList": [{
"acComTransRuleDefnEntity": {
"acComTransRuleDetailEntityList": [{
"detailSort": 1,
"effectiveTime": -9223372036854776000,
"exeConditionExps": "",
"expirationTime": 9223372036854776000,
"ruleDefnId": 529,
"targetPath": ""
}, {
"detailSort": 2,
"effectiveTime": -9223372036854776000,
"exeConditionExps": "",
"expirationTime": 9223372036854776000,
"ruleDefnId": 529,
"targetPath": "",
"targetValueExps": ""
}],
"effectiveTime": -9223372036854776000,
"expirationTime": 9223372036854776000,
"id": 529
},
"comTransRuleDefnId": 529,
"effectiveTime": -9223372036854776000,
"expirationTime": 9223372036854776000,
"id": 1240,
"nodeType": "common_transfer_rule",
"pressId": 361,
"stdBillOper": "",
"stdBillType": ""
}],
"id": 361,
"pressColId": 0,
"pressToNormalFunctionBean": "",
"runInStyles": 2,
"taskId": 0
}
新建一个类:CurrentRecognised,该插件有快捷键,Mac是option + s 即可弹出下面框,但是如果你本身工具已经存在该快捷键,那么可以该快捷键会失效,这种情况下,要通过在该类中,右键,在弹出的对话框中选择Generate,选择点击后会弹出GET、SET等,我们选择GsonFormat,如下图
Format
将我们提前准备好的JSON数据,复制到框中,在对话框的右上角,还可以进行格式化
Setting
在右下角有个Setting按钮,点开后,弹出如下对话框
默认情况下Convert Library 选项是Other ,生成的对象是get set形式,我们可以选择其他选项,例如Lombok,那么会将生成的对象带有Lombok的形式,代码看起来比较整洁,其他选项大家自己体验吧,功能还是有一些的。点击Ok后
我们能修改字段类型、映射后的字段名称,例如我们将 第一个字段id 修改成newId,点击OK后,生成的代码如下
@NoArgsConstructor
@Data
public class CurrentRecognised {
/**
* acIdttbPressFilterConfEntities : []
* acIdttbPressNodeConfEntityList : [{"acComTransRuleDefnEntity":{"acComTransRuleDetailEntityList":[{"detailSort":1,"effectiveTime":"-9223372036854776000","exeConditionExps":"","expirationTime":"9223372036854776000","ruleDefnId":529,"targetPath":""},{"detailSort":2,"effectiveTime":"-9223372036854776000","exeConditionExps":"","expirationTime":"9223372036854776000","ruleDefnId":529,"targetPath":"","targetValueExps":""}],"effectiveTime":"-9223372036854776000","expirationTime":"9223372036854776000","id":529},"comTransRuleDefnId":529,"effectiveTime":"-9223372036854776000","expirationTime":"9223372036854776000","id":1240,"nodeType":"common_transfer_rule","pressId":361,"stdBillOper":"","stdBillType":""}]
* id : 361
* pressColId : 0
* pressToNormalFunctionBean :
* runInStyles : 2
* taskId : 0
*/
private int newId;
private int pressColId;
private String pressToNormalFunctionBean;
private int runInStyles;
private int taskId;
private List<?> acIdttbPressFilterConfEntities;
private List<AcIdttbPressNodeConfEntityListBean> acIdttbPressNodeConfEntityList;
@NoArgsConstructor
@Data
public static class AcIdttbPressNodeConfEntityListBean {
/**
* acComTransRuleDefnEntity : {"acComTransRuleDetailEntityList":[{"detailSort":1,"effectiveTime":"-9223372036854776000","exeConditionExps":"","expirationTime":"9223372036854776000","ruleDefnId":529,"targetPath":"","targetValueExps":""},{"detailSort":2,"effectiveTime":"-9223372036854776000","exeConditionExps":"","expirationTime":"9223372036854776000","ruleDefnId":529,"targetPath":"","targetValueExps":""}],"effectiveTime":"-9223372036854776000","expirationTime":"9223372036854776000","id":529}
* comTransRuleDefnId : 529
* effectiveTime : -9223372036854776000
* expirationTime : 9223372036854776000
* id : 1240
* nodeType : common_transfer_rule
* pressId : 361
* stdBillOper :
* stdBillType :
*/
private AcComTransRuleDefnEntityBean acComTransRuleDefnEntity;
private int comTransRuleDefnId;
private String effectiveTime;
private String expirationTime;
private int id;
private String nodeType;
private int pressId;
private String stdBillOper;
private String stdBillType;
@NoArgsConstructor
@Data
public static class AcComTransRuleDefnEntityBean {
/**
* acComTransRuleDetailEntityList : [{"detailSort":1,"effectiveTime":"-9223372036854776000","exeConditionExps":"","expirationTime":"9223372036854776000","ruleDefnId":529,"targetPath":""},{"detailSort":2,"effectiveTime":"-9223372036854776000","exeConditionExps":"","expirationTime":"9223372036854776000","ruleDefnId":529,"targetPath":"","targetValueExps":""}]
* effectiveTime : -9223372036854776000
* expirationTime : 9223372036854776000
* id : 529
*/
private String effectiveTime;
private String expirationTime;
private int id;
private List<AcComTransRuleDetailEntityListBean> acComTransRuleDetailEntityList;
@NoArgsConstructor
@Data
public static class AcComTransRuleDetailEntityListBean {
/**
* detailSort : 1
* effectiveTime : -9223372036854776000
* exeConditionExps :
* expirationTime : 9223372036854776000
* ruleDefnId : 529
* targetPath :
* targetValueExps :
*/
private int detailSort;
private String effectiveTime;
private String exeConditionExps;
private String expirationTime;
private int ruleDefnId;
private String targetPath;
private String targetValueExps;
}
}
}
}
可以看到确实将id替换成了newId,这个插件在一些特定场合还是比较实用的,大家觉得如何呢