ResponseJsonUtil

 
public class ResponseJsonUtil {
public static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    static {
        FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    }

    /**
     * 响应过滤器
     */
    public static final class ResponseFilter extends ExtFilter implements ValueFilter {
        @Override
        public Object process(Object object, String name, Object value) {
            if (name.equals("createTime") || name.equals("modifyTime")) {
                return value;
            } else if (value instanceof Date) {
                return FORMAT.format(value);
            } else {
                return value;
            }
        }
    }

    /**
     *
     * @param responseCode
     * @return
     */
    public static String backJson4HttpApi(ResponseCode responseCode) {
        if (responseCode != null) {
            String result = JSON.toJSONString(responseCode, new ResponseFilter(),
                    SerializerFeature.DisableCircularReferenceDetect,
                    SerializerFeature.WriteNonStringKeyAsString);
            result = result.replaceFirst("\"data\":\\{", "");
            return result.substring(0, result.length() - 1);
        }
        return null;
    }

    /**
     * 验证失败的json串
     * @param code
     * @return
     */
    public static String backJson4VerifyFailure(int code) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("message", "未通过验证");
        return JSON.toJSONString(new ResponseCode(code, map), new ResponseFilter(),
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteNonStringKeyAsString);
    }

    /**
     * 成功的json串
     * @param responseCode
     * @return
     */
    public static String backJson(ResponseCode responseCode) {
        if (responseCode != null) {
            return JSON.toJSONString(responseCode, new ResponseFilter(),
                    SerializerFeature.DisableCircularReferenceDetect,
                    SerializerFeature.WriteNonStringKeyAsString);
        }
        return null;
    }
}
public class ResponseCode {

    public final int code;
    public final Object data;

    /**
     *
     * @param code
     * @param data
     */
    @JSONCreator
    public ResponseCode(@JSONField(name = "code") int code, @JSONField(name = "data")Object data) {
        this.code = code;
        this.data = data;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值