一、支付宝消息模板大致长这样

{
"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对象转支付宝/微信模板消息的更多相关文章

  1. java开发微信模板消息推送

    发布时间:2018-12-12   技术:springboot+maven   概述 该demo主要涉及微信模板消息推送功能, 详细 代码下载:http://www.demodashi.com/dem ...

  2. 前后端分离djangorestframework—— 接入微信模板消息推送

    微信 什么是微信也不多说,跟前面的支付宝一样的 微信支付 微信支付也有个沙箱环境,沙箱环境官方文档 由文档中那句很显眼的话所得,即使是测试环境也需要真实的商户号,所以这个就没法想支付宝那样用沙箱账号来 ...

  3. 【原创分享·微信支付】C# MVC 微信支付之微信模板消息推送

    微信支付之微信模板消息推送                    今天我要跟大家分享的是“模板消息”的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信生存的呀,所以他能不 ...

  4. C# MVC 微信支付之微信模板消息推送

    微信支付之微信模板消息推送                    今天我要跟大家分享的是"模板消息"的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信 ...

  5. 5分钟连续出现某现象+微信模板消息提醒 PHP

    需求场景:用电插座电流连续出现5次电流过高(大于 3A)后停止用电服务,前四次发送电流过高提醒,最后一次发送结束用电服务提醒 思路: Redis  key 设为:插座编号+user户编号  value ...

  6. 使用jackson对Java对象与JSON字符串相互转换的一些总结

    本文为菠萝大象原创,如要转载请注明出处.http://www.blogjava.net/bolo 代码无真相,为了最简单的说明,我直接上代码. public class User { private  ...

  7. JackSon将java对象转换为JSON字符串

    JackSon可以将java对象转换为JSON字符串,步骤如下: 1.导入JackSon 的jar包 2.创建ObjectMapper对象 3.使用ObjectMapper对象的writeValueA ...

  8. (后端)JackSon将java对象转换为JSON字符串(转)

    转载小金金金丶园友: JackSon可以将java对象转换为JSON字符串,步骤如下: 1.导入JackSon 的jar包 2.创建ObjectMapper对象 3.使用ObjectMapper对象的 ...

  9. json工具性能比较:json-lib和jackson进行Java对象到json字符串序列化[转]

    网上查找“java json”,发现大家使用最多的还是json-lib来进行java对象的序列化成json对象和反序列化成java对象的操作.但是之前在网上也看到过一往篇关于json序列化性能比较的文 ...

随机推荐

  1. linux笔记_day05

    1.bash以及特性 shell:外壳 GUI:KDE,Gnome,Xfce CLI:sh,csh,ksh,bash(born again shell) 进程:在每个进程看来,当前主机上只存在内核和当 ...

  2. jQuery中对未来的元素绑定事件用bind、live or on

    对未来的元素绑定事件不能用bind, 1.可以用live代替,但是要注意jquery的版本,根据官方文档,从1.7开始就不推荐live和delegate了,1.9里就去掉live了. 2.推荐用on代 ...

  3. 关于python中的module

    python中的module(模块),关于这个概念以及使用时主要有以下几点需要注意: (1)import xx时,会首先将这个xx module中的代码执行一遍(且仅执行一遍): 例如: (2)模块包 ...

  4. Linux input子系统简介

    1.前言 本文主要对Linux下的input子系统进行介绍 2. 软件架构 图 input子系统结构图 input子系统主要包括三个部分:设备驱动层.核心层和事件层.我们可以分别理解为:具体的输入设备 ...

  5. C语言函数调用栈(二)

    5 函数调用约定 创建一个栈帧的最重要步骤是主调函数如何向栈中传递函数参数.主调函数必须精确存储这些参数,以便被调函数能够访问到它们.函数通过选择特定的调用约定,来表明其希望以特定方式接收参数.此外, ...

  6. arm-linux-gcc/ld/objcopy/objdump参数总结【转】

    arm-linux-gcc/ld/objcopy/objdump参数总结 转自:http://blog.csdn.net/muyuyuzhong/article/details/7755291 arm ...

  7. Hacker需要掌握的基础

    编译语言:1.C语言能力要求:精通选用教材:<C Primer Plus 中文版(第5版)>其他教材:<标准C程序设计(第3版)><C语言入门经典(原书第3版)>补 ...

  8. 研究slatstack时踩过的坑,注意点及解决方案

    运行问题 1.直接物理性移除minion或者更换minion原先连接的master,会导致先前的master始终无法ping通minion [root@localhost salt]# salt '* ...

  9. OA系统高性能解决方案(史上最全的通达OA系统优化方案)

    序: 这是一篇针对通达OA系统的整体优化方案,文档将硬件.网络.linux操作系统.程序本身(包括web和数据库)以及现有业务有效结合在一起,进行了系统的整合优化.该方案应用于真实生产环境,部署完成后 ...

  10. Python-垃圾回收机制

    引子: 我们定义变量会申请内存空间来存放变量的值,而内存的容量是有限的,当一个变量值没有用了(简称垃圾)就应该将其占用的内存给回收掉,而变量名是访问到变量值的唯一方式,所以当一个变量值没有关联任何变量 ...