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 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 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 getKeywords() {
        return keywords;
    }

    public void setKeywords(List 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 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> {

    @Override
    public void serialize(List 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对象转支付宝/微信模板消息)