java返回json设置自定义的格式


  • 使用注解@JsonSerialize(using = CustomPriceSerialize.class)

  • 创建自定义的格式化类(可为内部类)

    
    /**
     * 设置默认返回的小数类型(0.01 元)
     */
    class CustomPriceSerialize extends JsonSerializer<BigDecimal> {
        private DecimalFormat df = new DecimalFormat("#0.00");
    
        public CustomPriceSerialize() {
        }
    
        public void serialize(BigDecimal value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            jgen.writeString(this.df.format(value));
        }
    }
    


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM