背景:目前想封装一个机器人推送的工具类 但是推送的消息体内部可能会有扩展 大家有什么比较好的封装思路 易于扩展 和简单的创建
实体类 类似于这样 可能中间后续还会有不同
{
"msg_type": "post",
"content": {
"post": {
"zh_cn": {
"title": "日报预警",
"content": [
[
{
"user_id": "12345679",
"tag": "at",
"text": "test"
},{
"user_id": "12222",
"tag": "at",
"text": "test"
}
]
]
}
}
}
}
目前的状态
创建出一个 推送的消息对象 需要多行的代码
List<List<ContentDetail>> tLists = new ArrayList<>();
ContentDetail contentDetail = new ContentDetail.Builder().setHerf("www.baidu.com").setTag("test").build();
ContentDetail contentDetail1 = new ContentDetail.Builder().setHerf("").build();
List<ContentDetail> list = new ArrayList<>();
list.add(contentDetail);
List<ContentDetail> list1 = new ArrayList<>();
list1.add(contentDetail1);
tLists.add(list);
tLists.add(list1);
BotPush botPush = new BotPush();
botPush.setMsg_type("post")
.setContent(new Content().setPost(new Post().setZh_cn(
new ZhCn().setTitle("111111").setContent(tLists))));
Object json = JSONObject.toJSON(botPush);
System.out.println(json);
我想要达到的结果
有木有什么好的思路可以减少一个实体类创建代码 以及后续的扩展