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

{
"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. 【Gradle】Gradle环境配置

    Windows环境 下载 作者下载的是gradle-4.1-all.zip,下载地址:http://services.gradle.org/distributions 环境配置 GRADLE_HOME ...

  2. joomla安装

    最开始我以为是我电脑反映慢.傻傻的等了很久.因为我在sae上面初始化成功了.只是差两张表而已.等了很久很久.也试了好几次.反正就是卡在创建数据表那里.突然我想到在sae初始化数据库的时候有两种模式In ...

  3. Apriori 算法python实现

    1. Apriori算法简介 Apriori算法是挖掘布尔关联规则频繁项集的算法.Apriori算法利用频繁项集性质的先验知识,通过逐层搜索的迭代方法,即将K-项集用于探察(k+1)项集,来穷尽数据集 ...

  4. 【shell】查找后拷贝find . -name *.csv -exec cp {} /home/ \;

    Find命令的一般形式为: find pathname -options [-print -exec -ok] 让我们来看看该命令的参数: pathname: find命令所查找的目录路径.例如用.来 ...

  5. Pcap4J实现抓包器

    前段时间搞抓包程序,打算使用Pcap4J实现,发现除了GitHub,其它资料少之又少,几乎都是不起作用. 被迫我一直看(日本作者!)英文注解的源码和sample和test,比较费劲+营养很少.因为几乎 ...

  6. 002_HTTP每日分析及翻译

    一.Request Headers Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng ...

  7. 转载:详解Java 自动装箱与拆箱的实现原理

    原文:http://www.jb51.net/article/111847.htm 什么是自动装箱和拆箱 自动装箱就是Java自动将原始类型值转换成对应的对象,比如将int的变量转换成Integer对 ...

  8. javascript判断是用什么设备打开

    var userAgentInfo = navigator.userAgent //查看浏览器用于 HTTP 请求的用户代理头的值 var agents = ["Android", ...

  9. 5个php实例,细致说明传值与传引用的区别

    传值:是把实参的值赋值给行参 ,那么对行参的修改,不会影响实参的值 传引用 :真正的以地址的方式传递参数传递以后,行参和实参都是同一个对象,只是他们名字不同而已对行参的修改将影响实参的值 说明: 传值 ...

  10. hdu3255扫描线:带权面积交转体积交

    手贱把i打成j,调了半天 /* 面积并转体积并,长方体高度为作物价格 算体积并:在笛卡尔坐标系的y轴上建立线段树cnt记录区间被完全覆盖的次数,sum记录区间被覆盖的总长度 以平行于xoy的平面从下往 ...