解决fastjson返回long 精度丢失的工具类(Solve the tool class of Long Precision missing returned by fastjson)

配合返回结果集代码,自己也可以封装到自己的结果集 【这个工具类只适用一些老项目使用,新项目可以参考配置springmvc统一格式返回,跟 注解方式】

package com.changh.sccms.utils;

import com.alibaba.fastjson.JSON;
import com.changh.sccms.api.rest.Result;
import com.changh.sccms.api.rest.ResultGenerator;
import com.google.common.collect.Lists;

import java.util.List;
import java.util.Map;

/**
 * @Classname JSONUtil
 * @Description TODO 解决 fastjson 精度丢失问题
 * @Date 2021/11/22/14:43
 * @Created by JiangHX
 */
public class Jtool extends JSON {

    @FunctionalInterface
    public interface ToJsonCallBack<T> {
        void callBack(Map<String, Object> m, T s);
    }

    /**
     * @param result 需要转换的对象
     * @param toJsonCallBack  转换方法
     * @Description: TODO  解决精度丢失 [字段转指定类型]
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @Author: JiangHX
     * @Date:2021/11/24 0024
     */
    public static <T> List<Map<String,Object>> toJSONStringPro(List<T> result,ToJsonCallBack<T> toJsonCallBack){
        List<Map<String,Object>> rs = Lists.newArrayList();
        if( null != toJsonCallBack ){
            result.forEach( e->{
                Map<String,Object> ms = parseObject(toJSONString(e),Map.class);
                toJsonCallBack.callBack(ms,e);
                rs.add(ms);
            });
        }
        return rs;
    }

    /**
     * @param result
     * @param toJsonCallBack
     * @Description: TODO  直接封装到返回对象
     * @return: com.changh.sccms.api.rest.Result
     * @Author: JiangHX
     * @Date:2021/11/24 0024
     */
    public static <T> Result toJSONStringResult(List<T> result, ToJsonCallBack<T> toJsonCallBack){
        List<Map<String,Object>> rs = Lists.newArrayList();
        if( null != toJsonCallBack ){
            result.forEach( e->{
                Map<String,Object> ms = parseObject(toJSONString(e),Map.class);
                toJsonCallBack.callBack(ms,e);
                rs.add(ms);
            });
        }
        return ResultGenerator.genLayuiSuccessResult(rs);
    }


}

使用方式:

//这是直接返回结果实体类Result
Jtool.toJSONStringResult(faddSignCategory, new Jtool.ToJsonCallBack<FaddSignCategory>() {
            @Override
            public void callBack(Map<String, Object> m, FaddSignCategory s) {
                //需要转换其它类型的字段
                m.put("id",String.valueOf(s.getId()));
            }
});
//这是返回List<Map> ,把方法名换一下就可以了
Jtool.toJSONStringPro(xxx)
————————

In combination with the returned result set code, you can also encapsulate it into your own result set [this tool class is only applicable to some old projects. For new projects, you can refer to the configuration of spring MVC unified format return, followed by annotation]

package com.changh.sccms.utils;

import com.alibaba.fastjson.JSON;
import com.changh.sccms.api.rest.Result;
import com.changh.sccms.api.rest.ResultGenerator;
import com.google.common.collect.Lists;

import java.util.List;
import java.util.Map;

/**
 * @Classname JSONUtil
 * @Description TODO 解决 fastjson 精度丢失问题
 * @Date 2021/11/22/14:43
 * @Created by JiangHX
 */
public class Jtool extends JSON {

    @FunctionalInterface
    public interface ToJsonCallBack<T> {
        void callBack(Map<String, Object> m, T s);
    }

    /**
     * @param result 需要转换的对象
     * @param toJsonCallBack  转换方法
     * @Description: TODO  解决精度丢失 [字段转指定类型]
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @Author: JiangHX
     * @Date:2021/11/24 0024
     */
    public static <T> List<Map<String,Object>> toJSONStringPro(List<T> result,ToJsonCallBack<T> toJsonCallBack){
        List<Map<String,Object>> rs = Lists.newArrayList();
        if( null != toJsonCallBack ){
            result.forEach( e->{
                Map<String,Object> ms = parseObject(toJSONString(e),Map.class);
                toJsonCallBack.callBack(ms,e);
                rs.add(ms);
            });
        }
        return rs;
    }

    /**
     * @param result
     * @param toJsonCallBack
     * @Description: TODO  直接封装到返回对象
     * @return: com.changh.sccms.api.rest.Result
     * @Author: JiangHX
     * @Date:2021/11/24 0024
     */
    public static <T> Result toJSONStringResult(List<T> result, ToJsonCallBack<T> toJsonCallBack){
        List<Map<String,Object>> rs = Lists.newArrayList();
        if( null != toJsonCallBack ){
            result.forEach( e->{
                Map<String,Object> ms = parseObject(toJSONString(e),Map.class);
                toJsonCallBack.callBack(ms,e);
                rs.add(ms);
            });
        }
        return ResultGenerator.genLayuiSuccessResult(rs);
    }


}

Usage:

//这是直接返回结果实体类Result
Jtool.toJSONStringResult(faddSignCategory, new Jtool.ToJsonCallBack<FaddSignCategory>() {
            @Override
            public void callBack(Map<String, Object> m, FaddSignCategory s) {
                //需要转换其它类型的字段
                m.put("id",String.valueOf(s.getId()));
            }
});
//这是返回List<Map> ,把方法名换一下就可以了
Jtool.toJSONStringPro(xxx)