杰克逊动态属性名称

huangapple go评论59阅读模式
英文:

Jackson dynamic property name

问题

我正在寻找一种智能解决方案,用于读取可能包含动态属性名称的HTTP响应中的JSON响应。

让我们考虑我要将JSON映射到的数据模型(Kotlin代码):

data class Response(
    @JsonProperty("id") val id: String,
    @JsonProperty("xyz_Value") val value: String
)

现在,这里的棘手部分是xyz_前缀对于value是动态的(将其视为环境变量,例如它可以是dev_Valueprod_Value)。

是否有现有的优雅解决方案我可以采用?

英文:

I am looking for a smart solution how to read a JSON response form a HTTP response, that could contain dynamic property name(s).

Let's consider a data model that I am mapping JSON into (Kotlin code):

data class Response(
    @JsonProperty("id") val id: String,
    @JsonProperty("xyz_Value") val value: String
)

Now, the tricky part here is that the xyz_ prefix for value is dynamic (treat it a environmental variable, so ie. it can be dev_Value or prod_Value)

Is there an existing elegant solution I can go with?

答案1

得分: 2

你可以编写一个自定义反序列化器 JsonDeserializer<Response>,并根据你的环境映射value字段。

英文:

You could write a custom deserializer JsonDeserializer<Response> and map the value field according to your environment

huangapple
  • 本文由 发表于 2020年7月22日 19:57:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63033648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定