Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @JsonProperty何时重命名对象字段?_Java_Json_Spring Boot - Fatal编程技术网

Java @JsonProperty何时重命名对象字段?

Java @JsonProperty何时重命名对象字段?,java,json,spring-boot,Java,Json,Spring Boot,我知道如何使用@JsonProperty将字段重命名为另一个名称,但它何时重命名对象字段?例如,notes被重命名为comment。 我试图阅读源代码,发现在com.fasterxml.jackson.databind.ser.std.BeanSerializerBase#serializeFields和com.fasterxml.jackson.databind.ser.BeanPropertyWriter#serializeAsField中有一些相关代码。但该字段已被重命名为comment。

我知道如何使用@JsonProperty将字段重命名为另一个名称,但它何时重命名对象字段?例如,
notes
被重命名为
comment

我试图阅读源代码,发现在
com.fasterxml.jackson.databind.ser.std.BeanSerializerBase#serializeFields
com.fasterxml.jackson.databind.ser.BeanPropertyWriter#serializeAsField
中有一些相关代码。但该字段已被重命名为
comment
。那么
@JsonProperty
重命名对象字段在哪里呢?

属性名称解析发生在
com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector
看看这个片段:

@Data
public class Tests {
    @JsonProperty("comment")
    private String notes;
}

所有这些都发生在
POJOPropertiesCollector.collectAll()

属性名称解析发生在
com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector
看看这个片段:

@Data
public class Tests {
    @JsonProperty("comment")
    private String notes;
}

所有这些都发生在
POJOPropertiesCollector.collectAll()中
我创建了一个简单的Pojo类

public PropertyName findNameForDeserialization(Annotated a) {
    ...
    // Get JsonProperty value for the field
    JsonProperty pann = (JsonProperty)this._findAnnotation(a, JsonProperty.class);
    if (pann != null) {
        // here we are !!!
        return PropertyName.construct(pann.value());
    } else {
        return !useDefault && !this._hasOneOf(a, ANNOTATIONS_TO_INFER_DESER) ? null : PropertyName.USE_DEFAULT;
    }
}
并运行ObjectMapper#writeValueAsString方法。调试了一段时间后,我发现Jackson用以下方法重命名了属性名

public class JsonTest {
  @JsonProperty("greetings")
  String hello;
}

我创建了一个简单的Pojo类

public PropertyName findNameForDeserialization(Annotated a) {
    ...
    // Get JsonProperty value for the field
    JsonProperty pann = (JsonProperty)this._findAnnotation(a, JsonProperty.class);
    if (pann != null) {
        // here we are !!!
        return PropertyName.construct(pann.value());
    } else {
        return !useDefault && !this._hasOneOf(a, ANNOTATIONS_TO_INFER_DESER) ? null : PropertyName.USE_DEFAULT;
    }
}
并运行ObjectMapper#writeValueAsString方法。调试了一段时间后,我发现Jackson用以下方法重命名了属性名

public class JsonTest {
  @JsonProperty("greetings")
  String hello;
}

到目前为止,我知道默认情况下有一个
对象映射器
对JSON对象进行序列化和反序列化,然后当
测试
对象用于序列化/反序列化时,映射器将JSON属性名称映射到带注释的Java字段的名称。到目前为止,我知道默认情况下有一个
对象映射器
序列化和反序列化一个JSON对象,然后当
Tests
对象用于序列化/反序列化时,映射器将JSON属性名称映射到带注释的Java字段名称。