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;
        }
    }

 

你可能感兴趣的:(java)