@JSONField位於alibaba的fastjson中。
JSONObject userJson = JSONObject.parseObject(userString);
User user = JSON.toJavaObject(userJson,User.class);
@JsonProperty位於jackson中
// jackson轉換工具
private static final ObjectMapper objectMapper = new ObjectMapper()
.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
String str = objectMapper.writeValueAsString(new MessageEntity());
List<String> tidList = objectMapper.readValue(tidListSrt, new TypeReference<List<String>>() {});
各種類型的轉換,詳情各api
這次主要遇到的是對es中的數據直接轉對象後,然後將對象返回給調用者,提供給調用者時,需遵循標準的駝峯,而不是ES中的下劃線。
對於要封裝的實體類,對象屬性中,可以使用@JsonPropertity(value=“user_name”)和@JSONField(name ="user_name")將es中的user_name通過fastjson或者jackson直接封裝成User對象。其屬性userName均是存在值,但是在通過前臺接口返回給調用者時,使用@JsonProperty時,給調用者的就是user_name,通過@JSONField註解時,調用者就是標準的userName駝峯格式。
所以在使用這個json轉換時,還是要注意區分一下的。。