Fastjson 格式化 BigDecimal

fastjson默认把 BigDecimal类型小数点后的 0 省略,如

BigDecimal a = new BigDecimal("18.283000")

//前端会直接输出

//18.283

//并不是我们期望的

//18.283000

解决方法一

使用@JSONField

public class CustomerBigDecimalCodec extends BigDecimalCodec implements ContextObjectSerializer {

    public final static CustomerBigDecimalCodec instance = new CustomerBigDecimalCodec();

    @Override
    public void write(JSONSerializer serializer, Object object, BeanContext context) {
        SerializeWriter out = serializer.out;
        if (object == null) {
            out.writeString("0.00");
            return;
        }
        String format = context.getFormat();
        System.out.println(format);
        DecimalFormat decimalFormat = new DecimalFormat(format);
        out.writeString(decimalFormat.format(object));
    }
}

fastjson配置

@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * fastjson全局配置
     * 
     * @param converters
     */
    @Override
    public void configureMessageConverters(List> converters) {
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setCharset(Charset.forName("UTF-8"));

        //BigDecimal格式化
        SerializeConfig serializeConfig = SerializeConfig.getGlobalInstance();
        serializeConfig.put(BigDecimal.class, CustomerBigDecimalCodec.instance);
        fastJsonConfig.setSerializeConfig(serializeConfig);


        fastJsonConverter.setFastJsonConfig(fastJsonConfig);
        List mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.APPLICATION_JSON);
        fastJsonConverter.setSupportedMediaTypes(mediaTypes);
        converters.add(fastJsonConverter);
    }
}

然后在需要格式化的类属性上指定format

public class Order {

    private Integer id;

    //小数点后面的0个数代表要保留的小数位
    @JSONField(format = "#0.00")
    private BigDecimal price;

}

这种方法虽然达到了我们想要的效果,但是有一定局限性,就是每个类需要格式化的属性都必须要指定format,如果能全局设定就好了,请往下看。

解决方法二

自定义Filter 重写 ValueFilter的 process 方法

public class BigDecimalValueFilter implements ValueFilter {

    @Override
    public Object process(Object object, String name, Object value) {
        if (value != null && value instanceof BigDecimal) {
            return String.format("%.6f", ((BigDecimal) value).doubleValue());
        }
        return value;
    }
}

通过设置序列化过滤器达到全局配置的效果

@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * fastjson全局配置
     * 
     * @param converters
     */
    @Override
    public void configureMessageConverters(List> converters) {
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setCharset(Charset.forName("UTF-8"));

     fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteNullStringAsEmpty);
        //设置我们自定义的 BigDecimalValueFilter
        fastJsonConfig.setSerializeFilters(new BigDecimalValueFilter());

        fastJsonConverter.setFastJsonConfig(fastJsonConfig);
        List mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.APPLICATION_JSON);
        fastJsonConverter.setSupportedMediaTypes(mediaTypes);
        converters.add(fastJsonConverter);
    }
}

测试前端输出,达到预期效果

 

你可能感兴趣的:(java,java,fastjson)