gpt4 book ai didi

java - 使用 MappingJackson2JsonView 更改显示名称的方法

转载 作者:行者123 更新时间:2023-11-30 04:16:21 25 4
gpt4 key购买 nike

我有一个 POJO 类结果

public class Result {
private String someName;
private String someOtherName;
}

它在我的模型中返回并在输出中显示为 json:

{"result":
{"someName":"value",
"someOtherName":"value"}}

我找不到将它们显示为 some_namesome_other_name 的方法,你知道必须设置什么样的映射才能做到这一点吗?

最佳答案

您可以使用@JsonProperty注释

public class Result {
@JsonProperty(value = "some_name")
private String someName;

@JsonProperty(value = "some_other_name")
private String someOtherName;
}

value 属性 javadoc 状态

Defines name of the logical property, i.e. JSON object field name to use for the property. If value is empty String (which is the default), will try to use name of the field that is annotated. Note that there is no default name available for constructor arguments, meaning that Empty String is not a valid value for constructor arguments.

关于java - 使用 MappingJackson2JsonView 更改显示名称的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18275330/

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