com.creditcloud.interestbearing.model.ConfigObjBase.java Source code

Java tutorial

Introduction

Here is the source code for com.creditcloud.interestbearing.model.ConfigObjBase.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.creditcloud.interestbearing.model;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.Serializable;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 *
 * @author guohuazhang
 */
@Slf4j
public class ConfigObjBase implements Serializable {

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE, false, false);
    }

    public String toJSON() {
        ObjectMapper mapper = new ObjectMapper();

        String json;
        try {
            json = mapper.writeValueAsString(this); //
            return json;
        } catch (JsonProcessingException ex) {
            log.error("?{}?JSON?", this, ex);
            return "{}";
        }
    }

    public static String toJSON(Object obj) {
        ObjectMapper mapper = new ObjectMapper();
        String json;
        try {
            json = mapper.writeValueAsString(obj); //
            return json;
        } catch (JsonProcessingException ex) {
            log.error("{}?JSON?", obj, ex);
            return "{}";
        }
    }
}