JsonProperty不会覆盖jackson从getter获得的默认名称.如果我用ObjectMapper和jackson序列化下面的类

{"hi":"hello"}

正如您所见,JsonProperty注释没有效果

class JacksonTester {
    String hi;

    @JsonProperty("hello")
    public String getHi() {
        return hi;
    }
}   

Putting @JsonProperty on the String itself doesn't work either. The only way it seems that I can change the name is by renaming the getter, the only problem is that it then will always be lowercase for the first letter

推荐答案

The problem was that I was using both the old and new jackson libraries

i、 e.在我之前

因为我在使用maven,这也意味着更新我的maven依赖项.

For it to work, I needed the @JsonProperty annotation on the getter (putting it on the object didn't work)

I found the answer here (thanks to francescoforesti) @JsonProperty not working as expected

Json相关问答推荐

Swift解码错误类型与`Bool`type不一致

筛选JSON数组以使用Jolt仅保留具有最新日期/时间的条目

抓取低于w jolt的对象级别

如何在VegaLite中应用Times New Roman,CaliBiri字体

无法从JSON解析ZonedDateTime,但可以使用格式化程序很好地解析

如何在Android中解析带有动态键和可变对象名称的改装JSON响应?

Rust实现:高效解析任意大小的JSON数组

Groovy JsonBuilder 在for循环中添加数组元素

使用 TypeScript 接口时如何修复未定义错误?

我无法将来自 API 的数据显示到 FlatList 中,但我在 console.log 中看到了它.问题是什么?

自定义将 struct 解组为切片映射

如何为所有 API 端点全局设置 http.ResponseWriter Content-Type 标头?

什么是类型和类型令牌?

Json.Net:用于自定义命名的 JsonSerializer-Attribute

在 JSON 编码的 HTML5 数据属性中转义/编码单引号

通过 RestAssured 中的 JsonPath 访问匿名数组的元素

使用 Python 3 读取 JSON 文件

Protocol Buffer vs Json - 何时 Select 一个而不是另一个

使用 JSONArray 和 JSONObject 进行 Foreach

为什么 jqXHR.responseText 返回字符串而不是 JSON 对象?