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

{
"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. EOF \n \0 NULL 之间的区别

    \n 是换行符 \0 是字符串的结束标志 EOF是流的结束标志 FILE* 这种流 NULL 是指针为空 第一个问题是EOF  它是end of file的缩写,表示"文字流"(s ...

  2. mysql原理~二阶段提交

    一 简介:今天咱们来聊聊 mysql 两阶段提交二 事务过程    perpare-commit 两个过程1  perpare阶段 redo日志   1.设置undo state=TRX_UNDO_P ...

  3. Java编程:悲观锁、乐观锁的区别及使用场景

    定义: 悲观锁(Pessimistic Lock): 每次获取数据的时候,都会担心数据被修改,所以每次获取数据的时候都会进行加锁,确保在自己使用的过程中数据不会被别人修改,使用完成后进行数据解锁.由于 ...

  4. Your Database is downloaded and backed up on....(腾讯云的mysql被攻击)

    今天发现自己的服务器被黑客攻击,自己的mysql服务器的库被删掉,并且新创了一个warning库,只有一个readme表.不知道原因,也许是自己再github上的项目暴漏了自己的密码,还要0.6比特币 ...

  5. Ubuntu/Debian 8 安装 Intel realsense 摄像头驱动

    ## Make Ubuntu/Debian Up-to-date1. sudo apt-get update && sudo apt-get upgrade && su ...

  6. 使用matplotlib绘制多个图形单独显示

    使用matplotlib绘制多个图形单独显示 一 代码 import numpy as np import matplotlib.pyplot as plt #创建自变量数组 x= np.linspa ...

  7. win7下出现读不到移动硬盘的解决办法

    很多电脑会出现移动硬盘读不到,或者 读到部分盘的情况,那么下面我就为大家来一一解决这些情况: 方法一:    最常见的是硬盘供电不足导致 ,要么换一个硬盘盒子,要么给硬盘一个外加电源即可方法二:    ...

  8. mysql删除数据库文件ibdata1后引发的故障

    进行性能测试是发现大量报错: Duplicate entry主键重复 可以看到mysql数据库中已经没有innodb引擎启动信息了 之前发现ibdata1占用了大量硬盘,为了省出空间删除了数据库ibd ...

  9. PYTHON-文件指针的移动,移动和函数基础

    # 文件内指针的移动 #大前提:文件内指针的移动是Bytes为单位的,唯独t模式下的read读取内容个数是以字符为单位 # f.seek(指针移动的字节数,模式控制): 控制文件指针的移动# 模式控制 ...

  10. PHP共享内存详解

    前言 在PHP中有这么一族函数,他们是对UNIX的V IPC函数族的包装. 它们很少被人们用到,但是它们却很强大.巧妙的运用它们,可以让你事倍功半. 它们包括: 信号量(Semaphores) 共享内 ...