是否有一個Jackson批註允許將數組反序列化到POJO的特定字段中?我可以用一個自定義的反序列化器很容易地做到這一點,但我希望能夠在課堂上完成這項工作。Jackson將數組元素反序列化爲特定字段
例如,我從Elasticsearch回來了以下JSON。
{
"_index": "twitter",
"_type": "tweet",
"_id": "AVodOsgk0etILSbJamY-",
"_score": null,
"_source": {
"tweetText": "I'm at Residencial Nova Alegria https:\/\/t.co\/4TyK8PAWzB",
"placeCountry": "Brasil",
"screenName": "wildelson",
"cleanedText": "I'm at Residencial Nova Alegria https:\/\/t.co\/4TyK8PAWzB",
"resolvedUrls": [
"https:\/\/www.swarmapp.com\/c\/gfULJdQ6umw"
],
"tweetId": "829272906291085314",
"tweetDate": 1486549042000
},
"sort": [
1486549042000,
"tweet#AVodOsgk0etILSbJamY-"
]
}
我POJO如下:
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Data;
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder(value = {
"enrichedStatus",
"sortValue",
"uid"
})
public class TweetHit {
private final EnrichedStatus enrichedStatus;
private final Long sortValue;
private final String uid;
public TweetHit(EnrichedStatus enrichedStatus, Long sortValue, String uid) {
this.enrichedStatus = enrichedStatus;
this.sortValue = sortValue;
this.uid = uid;
}
我想要的「排序」陣列,這將總是有一個很長的陣列[0]和在陣列的字符串[1],待反序列化如下:
Long sortValue = sort[0]
String uid = sort[1]
我發現一個問題要問,這裏唯一的答案是一個自定義解串器,這是我想避免,如果我能。 Jackson: Deserialize JSON-Array in different attributes of an object
我想也許我可以使用@JsonProperty("sort")
與@JsonFormat(shape = ARRAY)
莫名其妙地,但我沒有看到反正指定特定單元格反序列化到每個字段。
請注意,這是一個不可變的對象,所以我可能需要解決方案與構造函數一致工作,雖然也許我可以刪除final,添加一個空的構造函數,將註釋放在字段級別並使用Lombok如果傑克遜可以直接設置字段,則禁用setters?
哇,我不敢相信我沒有看到。我有兩個領域,因此有2個參數的隧道願景。這工作。謝謝! – Brooks