@JsonSerialize的使用
前后端对状态类字段不统一,需转换
@JsonSerialize注解,主要用于数据转换,该注解作用在该属性的getter()方法上。
public class MySerializerUtils extends JsonSerializer<Integer> { @Override public void serialize(Integer status, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { String statusStr = ""; switch (status) { case 0: statusStr = "暂存"; break; case 1: statusStr = "待上报"; break; case 2: statusStr = "待审核"; break; case 3: statusStr = "已审"; break; case 4: statusStr = "退回"; break; case -1: statusStr = "已删"; break; default: statusStr = "状态信息不符合"; } jsonGenerator.writeString(statusStr); } }
/** * 文字型状态值 */ @JsonSerialize(using = MySerializerUtils.class) private int status;