阻止Jackson更改变量名的大小写 [英] Stop Jackson from changing case of variable names

查看:233
本文介绍了阻止Jackson更改变量名的大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring中使用Jackson来将我的类序列化为JSON.当我像下面的示例一样序列化一个类时,Jackson会将某些字段的名称从Camel Case更改为小写.我知道我可以通过创建自定义(反)序列化器来解决此问题,但我希望在全球范围内将其关闭.也许通过在application.properties中设置一个属性.

I am using Jackson in Spring to serialize my class to JSON. When I serialize a class like the example below, Jackson is changing the names of certain fields from Camel Case to lower case. I know I can work around this by creating custom (de)serializers, yet I am hoping to turn this off globally. Perhaps by setting a property in application.properties.

根据

Per the default Jackson naming strategy, this is not supposed to happen:

在没有注册的自定义策略的情况下,将使用默认的Java属性命名策略,该字段将字段名称保留为...

In absence of a registered custom strategy, default Java property naming strategy is used, which leaves field names as is...

class Foo {
    private final String firstName;
    private final String aName;
    private final String name;
    private final String abName;

    Foo(final String firstName, final String aName, final String name, final String abName) {
        this.firstName = firstName;
        this.aName = aName;
        this.name = name;
        this.abName = abName;
    }
    // Getters here
}

public static void main(String[] args) {
        final ObjectMapper mapper = new ObjectMapper();
        final Foo foo  = new Foo("first", "a", "name", "ab");
        final String jsonInString = mapper.writeValueAsString(foo);
        System.out.println(jsonInString);
}

预期:

{"firstName":"first","name":"name","abName":"ab"," aName ":"a" ;}

{"firstName":"first","name":"name","abName":"ab","aName":"a"}

实际:

{"firstName":"first","name":"name","abName":"ab"," aname ":"a" ;}

{"firstName":"first","name":"name","abName":"ab","aname":"a"}

将问题缩小到对吸气剂的解释.开始看起来像杰克逊中的错误.

Narrowed the problem down to interpretation of the getters. Starting to look like a bug in Jackson.

class Foo {
    private final String aName;

    Foo(final String aName) {
        this.aName = aName;
    }

    public String getaName() {
        return this.aName;
    }
}

序列化为{"aName":"a"}

但是,

class Foo {
    private final String aName;

    Foo(final String aName) {
        this.aName = aName;
    }

    public String getAName() {
        return this.aName;
    }
}

序列化为{"aname":"a"}

推荐答案

这里的问题更多地是关于

The problem here is more about JavaBeans(TM) Specification. According to the spec (page 58)

但是,为了支持偶尔使用所有大写名称,我们 检查名称的前两个字符是否均为大写字母 如果是这样,就不要管它

However to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone

"FooBah"成为"fooBah"

"FooBah" becomes "fooBah"

"Z"变成"z"

"URL"变为"URL"

"URL" becomes "URL"

并且您有一个带有aName field的边套.因为如果吸气剂看起来像AName(),那么当您从该吸气剂转换回去时,您应该根据规范查找字段 .这是其他信息解释

And you have an edge case with aName field . Because if the getter looks like AName() , then when you convert back from this getter you should look for AName field according to the specification. Here is additional info explanation

因此,要解决此问题,可以在字段/getter上方使用适当的getter(getaName())或@JsonProperty("aName")批注

So to fix this, you can use a proper getter(getaName()) or @JsonProperty("aName") annotation above field/getter

这里是类似的问题

这篇关于阻止Jackson更改变量名的大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆