jackson实现java对象转支付宝/微信模板消息
一、支付宝消息模板大致长这样
{
"to_user_id": "",
"telephone": "xxxxx",
"template": {
"template_id": "xxxxxx",
"context": {
"head_color": "#85be53",
"url": "www.baidu.com",
"action_name": "查看详情",
"keyword1": {
"color": "#85be53",
"value": "15700000000"
},
"keyword2": {
"color": "#85be53",
"value": "2017年06月"
},
"keyword3": {
"color": "#85be53",
"value": "99.99"
},
"keyword4": {
"color": "#85be53",
"value": "66.66"
},
"first": {
"color": "#85be53",
"value": "您好,本月账单已出"
},
"remark": {
"color": "#85be53",
"value": "谢谢使用。"
}
}
}
}
二、java pojo
Item实体 TemplateMessageItem.java
public class TemplateMessageItem {
private String value;
private String color;
public TemplateMessageItem(String value, String color) {
this.value = value;
this.color = color;
}
public TemplateMessageItem(String value) {
this.value = value;
this.color = "#113d83";
}
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
public String getColor() {
return this.color;
}
public void setColor(String color) {
this.color = color;
}
}
最外层:TemplateMessage .java
import com.fasterxml.jackson.annotation.JsonProperty; import java.util.ArrayList;
import java.util.List;
import java.util.Objects; /**
* @author hujunzheng
* @create 2018-07-11 20:47
**/
public class TemplateMessage {
@JsonProperty("to_user_id")
private String toUserId = "";
private String telephone = "";
private NestTemplate template = new NestTemplate(); public String getToUserId() {
return toUserId;
} public void setToUserId(String toUserId) {
this.toUserId = toUserId;
} public String getTelephone() {
return telephone;
} public void setTelephone(String telephone) {
this.telephone = telephone;
} public NestTemplate getTemplate() {
return template;
} public void setTemplate(NestTemplate template) {
this.template = template;
} public TemplateMessage withToUserId(String toUserId) {
this.toUserId = toUserId;
return this;
} public TemplateMessage withTelephone(String telephone) {
this.telephone = telephone;
return this;
} public TemplateMessage withTemplateId(String templateId) {
this.template.setTemplateId(templateId);
return this;
} public TemplateMessage withContextHeadColor(String color) {
this.template.getContext().setHeadColor(color);
return this;
} public TemplateMessage withContextUrl(String url) {
this.template.getContext().setUrl(url);
return this;
} public TemplateMessage withContextActionName(String actionName) {
this.getTemplate().getContext().setActionName(actionName);
return this;
} public TemplateMessage withContextFirst(TemplateMessageItem first) {
this.getTemplate().getContext().setFirst(first);
return this;
} public TemplateMessage withContextRemark(TemplateMessageItem remark) {
this.getTemplate().getContext().setRemark(remark);
return this;
} public TemplateMessage addContextKeyword(TemplateMessageItem keyword) {
List<TemplateMessageItem> keywords = this.getTemplate().getContext().getKeywords();
if (Objects.isNull(keyword)) {
keywords = new ArrayList<>();
this.getTemplate().getContext().setKeywords(keywords);
}
keywords.add(keyword);
return this;
}
}
第一个嵌套层:NestTemplate.java
import com.fasterxml.jackson.annotation.JsonProperty;
public class NestTemplate {
@JsonProperty("template_id")
private String templateId;
private NestContext context = new NestContext();
public String getTemplateId() {
return templateId;
}
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
public NestContext getContext() {
return context;
}
public void setContext(NestContext context) {
this.context = context;
}
}
第二个嵌套层:NestContext.java
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize; import java.util.List; public class NestContext {
@JsonProperty("head_color")
private String headColor = "#85be53";
private String url;
@JsonProperty("action_name")
private String actionName;
private TemplateMessageItem first;
private TemplateMessageItem remark; @JsonProperty("keyword1")
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonSerialize(using = TemplateMessageItemsSerializer.class)
private List<TemplateMessageItem> keywords; public String getHeadColor() {
return headColor;
} public void setHeadColor(String headColor) {
this.headColor = headColor;
} public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} public String getActionName() {
return actionName;
} public void setActionName(String actionName) {
this.actionName = actionName;
} public TemplateMessageItem getFirst() {
return first;
} public void setFirst(TemplateMessageItem first) {
this.first = first;
} public TemplateMessageItem getRemark() {
return remark;
} public void setRemark(TemplateMessageItem remark) {
this.remark = remark;
} public List<TemplateMessageItem> getKeywords() {
return keywords;
} public void setKeywords(List<TemplateMessageItem> keywords) {
this.keywords = keywords;
} public static void main(String[] args) throws JsonProcessingException {
NestContext context = new NestContext();
context.setFirst(new TemplateMessageItem("first"));
context.setRemark(new TemplateMessageItem("remark"));
context.setUrl("www.baidu.com");
context.setActionName("查看详情");
// List<TemplateMessageItem> keywords = new ArrayList<>();
// keywords.add(new TemplateMessageItem("充值金额"));
// keywords.add(new TemplateMessageItem("手机号"));
// context.setKeywords(keywords);
System.out.println(new ObjectMapper().writeValueAsString(context));
}
} main方法中有注释:
{
"url":"www.baidu.com",
"first":{
"value":"first",
"color":"#113d83"
},
"remark":{
"value":"remark",
"color":"#113d83"
},
"head_color":"#85be53",
"action_name":"查看详情"
} main方法中无注释
{
"url":"www.baidu.com",
"first":{
"value":"first",
"color":"#113d83"
},
"remark":{
"value":"remark",
"color":"#113d83"
},
"head_color":"#85be53",
"action_name":"查看详情",
"keyword1":{
"value":"充值金额",
"color":"#113d83"
},
"keyword2":{
"value":"手机号",
"color":"#113d83"
}
}
三、自定义字段序列化
将一个List中的每个对象输出为多个并列json key=value的形式,当然要靠JsonSerializer啦!
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.util.CollectionUtils; import java.io.IOException;
import java.util.List; /**
* @author hujunzheng
* @create 2018-07-11 21:30
**/
public class TemplateMessageItemsSerializer extends JsonSerializer<List<TemplateMessageItem>> { @Override
public void serialize(List<TemplateMessageItem> items, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
if (CollectionUtils.isEmpty(items)) {
return;
} gen.writeObject(items.get(0)); for (int i = 1; i < items.size(); ++i) {
gen.writeFieldName("keyword" + (i + 1));
gen.writeObject(items.get(i));
}
}
}
jackson实现java对象转支付宝/微信模板消息的更多相关文章
- java开发微信模板消息推送
发布时间:2018-12-12 技术:springboot+maven 概述 该demo主要涉及微信模板消息推送功能, 详细 代码下载:http://www.demodashi.com/dem ...
- 前后端分离djangorestframework—— 接入微信模板消息推送
微信 什么是微信也不多说,跟前面的支付宝一样的 微信支付 微信支付也有个沙箱环境,沙箱环境官方文档 由文档中那句很显眼的话所得,即使是测试环境也需要真实的商户号,所以这个就没法想支付宝那样用沙箱账号来 ...
- 【原创分享·微信支付】C# MVC 微信支付之微信模板消息推送
微信支付之微信模板消息推送 今天我要跟大家分享的是“模板消息”的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信生存的呀,所以他能不 ...
- C# MVC 微信支付之微信模板消息推送
微信支付之微信模板消息推送 今天我要跟大家分享的是"模板消息"的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信 ...
- 5分钟连续出现某现象+微信模板消息提醒 PHP
需求场景:用电插座电流连续出现5次电流过高(大于 3A)后停止用电服务,前四次发送电流过高提醒,最后一次发送结束用电服务提醒 思路: Redis key 设为:插座编号+user户编号 value ...
- 使用jackson对Java对象与JSON字符串相互转换的一些总结
本文为菠萝大象原创,如要转载请注明出处.http://www.blogjava.net/bolo 代码无真相,为了最简单的说明,我直接上代码. public class User { private ...
- JackSon将java对象转换为JSON字符串
JackSon可以将java对象转换为JSON字符串,步骤如下: 1.导入JackSon 的jar包 2.创建ObjectMapper对象 3.使用ObjectMapper对象的writeValueA ...
- (后端)JackSon将java对象转换为JSON字符串(转)
转载小金金金丶园友: JackSon可以将java对象转换为JSON字符串,步骤如下: 1.导入JackSon 的jar包 2.创建ObjectMapper对象 3.使用ObjectMapper对象的 ...
- json工具性能比较:json-lib和jackson进行Java对象到json字符串序列化[转]
网上查找“java json”,发现大家使用最多的还是json-lib来进行java对象的序列化成json对象和反序列化成java对象的操作.但是之前在网上也看到过一往篇关于json序列化性能比较的文 ...
随机推荐
- D - 文理分科 (网络流->最小割)
题目链接:https://cn.vjudge.net/contest/281959#problem/D 题目大意:中文题目 具体思路:我们需要求出最大的满意值,从另一方面想,我们可以求出总的满意值,然 ...
- dp之免费馅饼
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 【C++】面试题目:从尾到头打印链表
通过<剑指offer 名企面试官精讲典型编程题>看到一道讲解链表的题目. 题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值 链表定义如下: typedef struct _NO ...
- 【转】PyDev Eclipse使用技巧说明
PyDev Package Explorer 创建项目 在开展工作之前,需要创建一个新的项目.在 Eclipse 菜单栏中,选择 File > New > Project > Pyd ...
- k64 datasheet学习笔记4---Memory Map
1.前言 本文主要介绍K64地址空间的映射 2. System Memory Map 3. K64地址映射 4. Armv7m地址映射 4.1 Armv7M.System地址段(0XE0000000~ ...
- git获取内核源码的方法
[转]http://www.360doc.com/content/17/0410/16/23107068_644444795.shtml 1. 前言 本文主要讲述ubuntu下通过git下载linux ...
- ajax异步请求302
我们知道,只有请求成功ajax才会进行回调处理,具体状态码为 status >= 200 && status < 300 || status === 304; 这一点通过查 ...
- zabbix3.0监控centos当主机cpu使用率超过90%的时候报警
在windows系统中监控cpu利用率非常容易,自带模板就有这样的功能,但是在linux里面没有默认的模板 只有cpu的负载,默认当cpu的负载在一定时间内5以上报警 cpu utilization中 ...
- Python-CSS进阶
0. 什么时候该用什么布局 <!-- 定位布局: 以下两种布局不易解决的问题, 盒子需要脱离文档流处理 --> <!-- 浮动布局: 一般有block特性的盒子,水平排列显示 --& ...
- PYTHON-基本数据类型-元祖类型,字典类型,集合类型-练习
# 1 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],# 将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中## 即: ...