作用:用於在序列化是加入開發者的代碼(這句話網上抄的),常用於對象或屬性上,前提是對象實現了Serializable接口
使用方法
1.作用於對象
@JsonSerialize(include = ''),include里面包含了序列化的范圍和作用的規則,例如@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)就是屬性為null的時候不進行序列化操作。
2.作用於屬性
@JsonSerialize(using = Bean.class),Bean 為實現類,Bean需要繼承JsonSerializer<>,泛型就是屬性的類型。
例如
public class JsonURLdecode extends JsonSerializer<String> { @Override public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { String result = URLDecoder.decode(value, "UTF-8"); gen.writeString(result); } }