工具类:
public class MySerializerUtils extends JsonSerializer<Integer> {
@Override
public void serialize(Integer value, JsonGenerator jsonGenerator, SerializerProvider serializers) throws IOException {
String statusStr = "";
switch (value) {
case 0:
statusStr = "Service Publish";
break;
case 1:
statusStr = "Configuration Upgrade";
break;
case 2:
statusStr = "Flow Cycle Deduction";
break;
case 3:
statusStr = "Service Cycle Deduction";
break;
case 4:
statusStr = " Add new public chain plans";
break;
default:
statusStr = "Public chain plan upgrade";
}
jsonGenerator.writeString(statusStr);
}
}
实体的注解:
@JsonSerialize(using = MySerializerUtils.class)
private Integer billType;
另外一种使用方式,格式化数据
@JsonSerialize(converter = CurrencyConverter.class)
private BigDecimal billAmount;`
public class CurrencyConverter implements Converter<BigDecimal, String> {
@Override
public String convert(BigDecimal bigDecimal) {
return NumberUtils.format(bigDecimal);
}
@Override
public JavaType getInputType(TypeFactory typeFactory) {
return typeFactory.constructType(BigDecimal.class);
}
@Override
public JavaType getOutputType(TypeFactory typeFactory) {
return typeFactory.constructType(String.class);
}
}
@JsonSerialize的使用
最新推荐文章于 2024-09-06 13:19:23 发布