Jackson 反序列化時實現大小寫不敏感設置
常用配置
ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(Feature.IGNORE_UNKNOWN,true); objectMapper.configure(Feature.WRITE_BIGDECIMAL_AS_PLAIN,true); objectMapper.configure(JsonParser.Feature.ALLOW_MISSING_VALUES,true); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false); objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES,false);//大小寫脫敏 默認為false 需要改為tru
參考
com.fasterxml.jackson.databind.MapperFeature#ACCEPT_CASE_INSENSITIVE_PROPERTIES
使用註解方式:舉例
public static void main(String[] args) throws IOException { String x = "{\n" + " \"TToUserName\":\"gh_a5624dd2db4e\",\n" + " \"FFromUserName\":\"ochvq0Kn35VlnTAcIJ3fRBAZTQUY\"" + " }"; ObjectMapper objectMapper = new ObjectMapper(); Result map = objectMapper.readValue(x, Result.class); System.out.println(map); objectMapper.writeValue(System.out,map); } private static class Result { private String ToUserName; private String FromUserName; @JsonProperty("ToUserName") public String getToUserName() { return ToUserName; } @JsonProperty("TToUserName") public void setToUserName(String toUserName) { ToUserName = toUserName; } @JsonProperty("FromUserName") public String getFromUserName() { return FromUserName; } @JsonProperty("FFromUserName") public void setFromUserName(String fromUserName) { FromUserName = fromUserName; } }
Jackson 轉換大小寫問題
Jackson轉換json時會把大寫轉換成小寫
解決辦法:
1、在變量時加上: @JsonProperty
2、在set/get方法加上:@JsonIgnore
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。
推薦閱讀:
- 詳解Java-Jackson使用
- 詳解Jackson的基本用法
- ObjectMapper 如何忽略字段大小寫
- Jackson庫中objectMapper的用法
- fastjson轉換對象實體@JsonProperty不生效問題及解決