全局设置
public class CustomerDoubleSerialize extends JsonSerializer {
private DecimalFormat df = new DecimalFormat("0.00");
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
if(o.toString() != null && !"-".equals(o)) {
Double dd=Double.parseDouble(o.toString());
jsonGenerator.writeString(df.format(dd));
} else{
jsonGenerator.writeString(o.toString());
}
}
}
在需要使用的实体字段上加上注解即可。@JsonSerialize(using = CustomerDoubleSerialize.class)
实体类加上注解。
@Data
public class SalesVolumeVo {
@JsonSerialize(using = CustomerDoubleSerialize.class)
private String salesVolume1;
}