我是靠谱客的博主 忧伤煎饼,最近开发中收集的这篇文章主要介绍jackson-PropertyNamingStrategy.SNAKE_CASE 遇到的问题与解决方案举例如下情景1情景2情景3情景4-暂时性解决方案情景5-解决方案【推荐】,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目录

举例如下

情景1

正常情况下

json字段

目标类

测试用例

输出结果

情景2

json字段-同1

目标类

测试用例-同1

输出结果

测试用例

开启PropertyNamingStrategy.SNAKE_CASE

输出结果

情景3

json字段-同1

目标类

测试用例-同2

输出结果

存在问题

情景4-暂时性解决方案

目标对象

测试用例

输出结果

情景5-解决方案【推荐】

 目标对象

测试用例

输出结果


举例如下

json里有命名为home_test的字段需要反序列化
可直接看情景5-解决方案

情景1

正常情况下

json字段

jsonString包含"home_test"字段

String newjson1 = "{"biztype":1236,"hometown":"11","hoMe":"hm","home_test":"hm_test_value"}";

目标类

注意这里的命名方式,有下划线

package json;

public class Obj {
    // 注意这里的命名方式
    private String home_test;
    public String getHome_test() {
        return home_test;
    }
    public void setHome_test(String home_test) {
        this.home_test = home_test;
    }
    @Override
    public String toString() {
        return "Obj{" +
                "home_test='" + home_test + ''' +
                '}';
    }
}

测试用例

package json;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

public class Test {
    public static void main(String[] args) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 注意是否开启
        // objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        String newjson1 = "{"biztype":1236,"hometown":"11","hoMe":"hm","home_test":"hm_test_value"}";

        Obj obj = objectMapper.readValue(newjson1, Obj.class);
        System.out.println(obj);
    }
}

输出结果

Obj{home_test='hm_test_value'}

情景2

json字段-同1

目标类

注意这里的命名方式,没有下划线

package json2;

public class Obj {
    // 注意这里的命名方式
    private String homeTest;
    public String getHomeTest() {
        return homeTest;
    }
    public void setHomeTest(String homeTest) {
        this.homeTest = homeTest;
    }
    @Override
    public String toString() {
        return "Obj{" +
                "homeTest='" + homeTest + ''' +
                '}';
    }
}

测试用例-同1

package json2;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Test {
    public static void main(String[] args) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 注意是否开启
        // objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        String newjson1 = "{"biztype":1236,"hometown":"11","hoMe":"hm","home_test":"hm_test_value"}";

        Obj obj = objectMapper.readValue(newjson1, Obj.class);
        System.out.println(obj);
    }
}

输出结果

Obj{homeTest='null'}

测试用例

开启PropertyNamingStrategy.SNAKE_CASE

// 注意是否开启
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
package json2;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

public class Test {
    public static void main(String[] args) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 注意是否开启
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        String newjson1 = "{"biztype":1236,"hometown":"11","hoMe":"hm","home_test":"hm_test_value"}";

        Obj obj = objectMapper.readValue(newjson1, Obj.class);
        System.out.println(obj);
    }
}

输出结果

Obj{homeTest='hm_test_value'}

情景3

注意!!!!坑快来了

PropertyNamingStrategy.SNAKE_CASE开启后,带来的新问题

json字段-同1

注意“hoMe”字段

String newjson1 = "{"biztype":1236,"hometown":"11","hoMe":"hm","home_test":"hm_test_value"}";

目标类

增加hoMe属性,注意home的命名方式,与json一致

package json3;

public class Obj {
    // 注意这里的命名方式
    private String homeTest;
    // 注意home的命名方式
    private String hoMe;
    public String getHomeTest() {
        return homeTest;
    }
    public void setHomeTest(String homeTest) {
        this.homeTest = homeTest;
    }
    public String getHoMe() {
        return hoMe;
    }
    public void setHoMe(String hoMe) {
        this.hoMe = hoMe;
    }
    @Override
    public String toString() {
        return "Obj{" +
                "homeTest='" + homeTest + ''' +
                ", hoMe='" + hoMe + ''' +
                '}';
    }
}

测试用例-同2

开启 objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);

package json3;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

public class Test {
    public static void main(String[] args) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 注意是否开启
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        String newjson1 = "{"biztype":1236,"hometown":"11","hoMe":"hm","home_test":"hm_test_value"}";

        Obj obj = objectMapper.readValue(newjson1, Obj.class);
        System.out.println(obj);
    }
}

输出结果

Obj{homeTest='hm_test_value', hoMe='null'}

存在问题


开启PropertyNamingStrategy.SNAKE_CASE,解决了home_test转homeTest问题,但是引入了hoMe转hoMe失败问题

情景4-暂时性解决方案

不开启PropertyNamingStrategy.SNAKE_CASE,并解决home_test转homeTest问题

目标对象

只新增了私有的setHome_test方法

package json4;

public class Obj {
    // 注意这里的命名方式
    private String homeTest;
    // 注意home的命名方式
    private String hoMe;
    public String getHomeTest() {
        return homeTest;
    }
    public void setHomeTest(String homeTest) {
        this.homeTest = homeTest;
    }
    // 新增了私有的setHome_test方法
    private void setHome_test(String homeTest) {
        this.homeTest = homeTest;
    }
    public String getHoMe() {
        return hoMe;
    }
    public void setHoMe(String hoMe) {
        this.hoMe = hoMe;
    }
    @Override
    public String toString() {
        return "Obj{" +
                "homeTest='" + homeTest + ''' +
                ", hoMe='" + hoMe + ''' +
                '}';
    }
}

测试用例

关闭PropertyNamingStrategy.SNAKE_CASE

package json4;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

public class Test {
    public static void main(String[] args) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 注意是否开启
        // objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        String newjson1 = "{"biztype":1236,"hometown":"11","hoMe":"hm","home_test":"hm_test_value"}";

        Obj obj = objectMapper.readValue(newjson1, Obj.class);
        System.out.println(obj);
    }
}

输出结果

Obj{homeTest='hm_test_value', hoMe='hm'}

情景5-解决方案【推荐】

开启PropertyNamingStrategy.SNAKE_CASE,并解决转换问题

直接字段映射注解。利用@JsonProperty映射

// 注意这里的命名方式
@JsonProperty(value = "home_test")
private String homeTest;
// 注意这里的命名方式
@JsonProperty(value = "hoMe")
private String hoMe;

 目标对象


public class Obj {
    // 注意这里的命名方式

    @JsonProperty(value = "home_test")
    private String homeTest;
    // 注意home的命名方式
   @JsonProperty(value = "hoMe")
    private String hoMe;
    public String getHomeTest() {
        return homeTest;
    }
    public void setHomeTest(String homeTest) {
        this.homeTest = homeTest;
    }
    public String getHoMe() {
        return hoMe;
    }
    public void setHoMe(String hoMe) {
        this.hoMe = hoMe;
    }
    @Override
    public String toString() {
        return "Obj{" +
                "homeTest='" + homeTest + ''' +
                ", hoMe='" + hoMe + ''' +
                '}';
    }

}

测试用例

开启PropertyNamingStrategy.SNAKE_CASE

 public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 注意是否开启
         objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        String newjson1 = "{"biztype":1236,"hometown":"11","hoMe":"hm","home_test":"hm_test_value"}";

        Obj obj = objectMapper.readValue(newjson1, Obj.class);
        System.out.println(obj);
    }

输出结果

Obj{homeTest='hm_test_value', hoMe='hm'}

最后

以上就是忧伤煎饼为你收集整理的jackson-PropertyNamingStrategy.SNAKE_CASE 遇到的问题与解决方案举例如下情景1情景2情景3情景4-暂时性解决方案情景5-解决方案【推荐】的全部内容,希望文章能够帮你解决jackson-PropertyNamingStrategy.SNAKE_CASE 遇到的问题与解决方案举例如下情景1情景2情景3情景4-暂时性解决方案情景5-解决方案【推荐】所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部