gpt4 book ai didi

java - Java中的getIdentifier()方法创建标识符字段?

转载 作者:行者123 更新时间:2023-12-02 05:38:01 25 4
gpt4 key购买 nike

在Java中,我有一个类,其中有一个返回字符串的公共(public)getIdentifier()方法。当我使用 jackson 序列化此类的实例时,生成的 json 字符串中会出现一个标识符字段。我的类中没有声明 identifier 字段,只有 getIdentifier 函数。 Java 中的 get@#%%() 函数是否会自动创建一个名为 @#%% 的字段,或者 Jackson 在序列化对象时可能会创建该字段本身?有办法解决这个问题吗?

最佳答案

Does having a get@#%%() function in Java automatically create a field named @#%%--or is Jackson possibly creating the field itself when serializing the object? Is there a way to get around this?

我可以保证,这不是 Java 的工作。 Jackson 是这里的罪魁祸首,它会使用所有 getter 方法将对象序列化为 JSON 字符串。如果您不想要这个,您应该使用 @JsonIgnore jackson 注释。

public class Foo {
//my fields go here...
//getters for each field go here as well...

//now this method will be ignored for Jackson JSON serialization
@JsonIgnore
public String getIdentifier() {
//fancy code that returns an identifier
}
}

关于java - Java中的getIdentifier()方法创建标识符字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24785013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com