[英]Custom Jackson mapper to handle mapping for an individual field?
我正在使用Jackson將Java模型對象分類為JSON,對於其中一個字段,讓我們說'title',我映射值的方式取決於另一個字段的值。 因此,它不是一個直接的映射。
我可以創建一個自定義映射器,在映射'title'字段時調用它,允許我添加必要的邏輯嗎?
自定義Date字段輸出的示例:
@JsonSerialize(using=CustomDateSerializer.class)
private Date timestamp;
CustomDateSerializer.class
public class CustomDateSerializer extends JsonSerializer<Date> {
public void serialize(Date value, JsonGenerator gen, SerializerProvider arg2) throws
IOException, JsonProcessingException {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy H:m:s");
String formattedDate = formatter.format(value);
gen.writeString(formattedDate);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.