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

{
"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. mysql基本命令[转]

    1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...

  2. 如何提交内核补丁--checkpatch.pl使用【转】

    转自:https://blog.csdn.net/qq_29350001/article/details/52056667 转自: http://blog.csdn.net/ganggexiongqi ...

  3. 织梦dedeCMS数据库结构字段说明-简略说明

    dede_addonarticle 附加文章表 aid int(11) 文章编号typeid int(11) 分类栏目编号body mediumtext 文章内容dede_addonflash 附加F ...

  4. tomcat启动报错 ERROR o.a.catalina.session.StandardManager 182 - Exception loading sessions from persiste

    系统:centos6.5 x86_64 jdk: 1.8.0_102 tomcat:8.0.37 tomcat 启动报错: ERROR o.a.catalina.session.StandardMan ...

  5. nagios系列(七)nagios通过自定义脚本的方式监控mysql主从同步

    nagios监控mysql主从同步 起因:nagios可能监控到mysql服务的运行情况,但确不能监控mysql的主从复制是否正常:有时候,同步已经停止,但管理人员却不知道. 登陆mysql从服务器, ...

  6. nagios系列(一)centos6.5环境部署nagios服务端

    nagios软件安装包存放目录:/home/oldboy/tools nagios服务安装目录:/usr/local/nagios 1.配置yum源 echo "------ step 1: ...

  7. Android service与Thread

    很多时候,你可能会问,为什么要用 Service,而不用 Thread 呢,因为用 Thread 是很方便的,比起 Service 也方便多了,下面我详细的来解释一下. 1). Thread:Thre ...

  8. Linux常用命令3(压缩和解压缩总结)

    tar命令 解包:tar zxvf FileName.tar 打包:tar czvf FileName.tar DirName gz命令 解压1:gunzip FileName.gz 解压2:gzip ...

  9. Appium+Java(一) Windows环境搭建篇

    准备: Android版本 :4.2.2 nodejs版本:5.6.0 appium版本:v1.4.16 1. 安卓SDK及配置环境变量 1.1.先下载sdk安装包:installer_r24.4.1 ...

  10. sklearn 岭回归

    可以理解的原理描述: [机器学习]岭回归(L2正则) 最小二乘法与岭回归的介绍与对比 多重共线性的解决方法之——岭回归与LASSO