Rob*_*pek 8 java json response jackson netflix-feign
我在休息时使用假装。不幸的是,我得到的其中一个回复看起来像这样:
{
"customer-id" : "0123"
}
Run Code Online (Sandbox Code Playgroud)
JSON 响应会自动映射到我的 POJO 之一。此响应对象不能具有名称为“customer-id”的属性字段,因为标识符名称中不允许使用破折号 (-)。
我尝试了以下方法:
public class LookUpAccountsResponse {
@JsonProperty("customer-id")
private String customerId;
}
Run Code Online (Sandbox Code Playgroud)
但不幸的是,这不起作用。有没有人有关于如何解决这个问题的建议?
com.google.gson.GsonDecoder
不确定为什么JsonProperty在您的类路径上,但请参阅“字段命名支持” https://github.com/google/gson/blob/master/UserGuide.md#json-field-naming-support
@SerializedName 是您想要的 Gson 注释
或者完全切换到使用feign-jackson依赖项JacksonDecoder
效果很好。这是一个最小的例子:
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
SomeClass sc = new ObjectMapper().readValue("{\"property-with-dash\": 5}", SomeClass.class);
System.out.println(sc.propertyWithDash);
}
public static class SomeClass {
@JsonProperty("property-with-dash")
private int propertyWithDash;
}
Run Code Online (Sandbox Code Playgroud)
这5将按预期打印。没什么好抱怨的。
| 归档时间: |
|
| 查看次数: |
8244 次 |
| 最近记录: |