如果开头的驼峰式单词只有一个字母长,为什么 Jackson 2 无法识别第一个大写字母? [英] Why does Jackson 2 not recognize the first capital letter if the leading camel case word is only a single letter long?

查看:73
本文介绍了如果开头的驼峰式单词只有一个字母长,为什么 Jackson 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"是小写的,而L"显然是属性的骆驼大小写约定中的第二个单词,并且打算大写?是不是因为第一个单词只有一个字母长?

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 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 either 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"时小写 Jackson 能够找出您想要映射的字段.

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

说了以上,您仍然可以选择使用aLogId"字段名称并使 Jackson 工作您所要做的就是使用带有 aLogId@JsonProperty 注释.

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

这篇关于如果开头的驼峰式单词只有一个字母长,为什么 Jackson 2 无法识别第一个大写字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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