【发布时间】:2022-01-14 12:23:01
【问题描述】:
我有一个 API 正在扩展以供另一个系统使用。 Pre-req 存在,每个前端对一个 json 字段有不同的期望。
示例: 响应字段“金额”必须是字符串或整数,具体取决于我从下游接收到的值。所以在某些情况下,我会在 json 中返回一个字符串值,而在其他情况下,我会返回 int。
预期的 json 输出:
{
"amount": 21
}
或
{
"amount": "21"
}
我做了以下事情:
class Response {
@JsonProperty("amount")
private int amount;
@JsonProperty("amount")
private String amountString;
// Getters and setters
希望我能够为 json 字段“金额”返回一个 int 或 String 值,但我收到以下错误:
com.fasterxml.jackson.databind.JsonMappingException:属性“amount”的getter定义冲突
任何帮助将不胜感激
【问题讨论】:
标签: java spring-boot serialization jackson