为什么杰克逊2不承认第一个大写字母,如果领先的骆驼案例字只有一个字母长? [英] Why does Jackson 2 not recognize the first capital letter if the leading camel case word is only a single letter long?

查看:155
本文介绍了为什么杰克逊2不承认第一个大写字母,如果领先的骆驼案例字只有一个字母长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring 4 MVC和Jackson 2进行我的服务。对于其中一个操作,我有一个请求对象,该对象具有一个属性,其中前导的驼峰案例字长度只有一个字母:

I'm using Spring 4 MVC with Jackson 2 for my service. For one of the operations I have a request object that has an attribute where the leading camel case word this is only one letter in length:

private String aLogId;

此类具有适当命名的getter和setter:

This class has the appropriately named getters and setters:

public String getALogId() { return aLogId; }
public void setALogId(String aLogId) { this.aLogId = aLogId; }

但是,当我尝试使用相应的JSON属性向此服务发布请求时:

However, when I attempt to post a request to this service using the corresponding JSON property:

{"aLogId":"This is a log id"}

我收到来自Spring框架的500响应,说该字段无法识别,我的控制器类永远不会被调用:

I'm receiving a 500 response from the Spring framework saying the field is not recognized and my controller class is never called:


无法读取JSON:无法识别的字段aLogId(类

Could not read JSON: Unrecognized field "aLogId" (class

然而,当我将L更改为小写,请求按预期反序列化并且我的控制器类被命中:

However, when I change the "L" to lower case, the request is deserialized as expected and my controller class is hit:

{"alogId":"This is a log id"}

杰克逊为什么期望L为小写当它显然是属性的camel case约定中的第二个单词并且意图是大写的?是因为第一个单词只有一个字母长吗?

Why does Jackson expect the "L" to be lower case when it is obviously the second word in the camel case convention for the attribute and intended to be in upper case? Is it because the first word is only a single letter long?

请求对象中还有其他属性第一个单词不止一个字母而那些归属不会遇到同样的问题,如果情况不匹配。

There are other attributes in the request object where the first word is more than one letter and those attributed don't face this same issue with the mismatch in case.

推荐答案

问题你看到的是由于Jackson使用Java Bean命名约定,在Java类中找出它的Json属性。

The problem you are seeing is due to the fact that Jackson uses Java Bean naming conventions, to figure it out the the Json properties in a Java class.

这是参考您看到的具体问题,建议不要将您所在领域的前两个字母都没有大写。如果您使用像IntelliJ或eclipse这样的IDE并让IDE为您生成setter,您会注意到会出现相同的行为,最终会得到以下方法:

Here is a reference of the specific problem you see, the recommendation is not to capitalize none of the first two letters in your field. If you use an IDE like IntelliJ or eclipse and let the IDE generate the setters for you, you will notice the same "behavior" occurs, you will end up with the following methods:

public void setaLogId(String aLogId) {
    this.aLogId = aLogId;
}

public String getaLogId() {
    return aLogId;
}

因此,当你将L改为小写时杰克逊能够弄清楚你想要映射的字段。

Hence, when you change the "L" to lower case Jackson was able to figure it out the field you wanted to map.

说完上面的话,你仍然可以选择使用aLogId字段名称让Jackson完成所有工作要做的是使用 @JsonProperty 注释,其中包含 aLogId

Having said the above, you still have the alternative to use the "aLogId" field name and make Jackson work all you have to do is use the @JsonProperty annotation with the aLogId in it.

@JsonProperty("aLogId")
private String aLogId;

以下测试代码用于说明这是如何工作的:

The following test code is to show how this will work:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Test {

    @JsonProperty("aLogId")
    private String aLogId;

    public void setaLogId(String aLogId) {
        this.aLogId = aLogId;
    }

    public String getaLogId() {
        return aLogId;
    }

    public static void main(String[] args) {

        ObjectMapper objectMapper = new ObjectMapper();

        Test test = new Test();

        test.setaLogId("anId");

        try {
            System.out.println("Serialization test: " + objectMapper.writeValueAsString(test));


            String json = "{\"aLogId\":\"anotherId\"}";

            Test anotherTest = objectMapper.readValue(json, Test.class);

            System.out.println("Deserialization test: " +anotherTest.getaLogId());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

输出测试是:

序列化测试:{aLogId:anId}

反序列化测试:anotherId

这篇关于为什么杰克逊2不承认第一个大写字母,如果领先的骆驼案例字只有一个字母长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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