【发布时间】:2026-01-07 04:55:01
【问题描述】:
我打电话给restTemplate:
restTemplate.exchange(uri, HttpMethod.GET, prepareHttpEntity(), MyDeserializedClass.class);
MyDeserializedClass:
public class MyDeserializedClass {
private final String id;
private final String title;
@JsonCreator
public MyDeserializedClass(@JsonProperty("id") String id,
@JsonProperty("title") String title) {
this.pageId = pageId;
this.title = title;
}
}
当 json 中没有对象时,我得到 MyDeserializedClass 和 null 值。
我尝试使用注释 MyDeserializedClass
@JsonInclude(JsonInclude.Include.NON_NULL) 或 @JsonIgnoreProperties(ignoreUnknown = true) 但没有运气。
有没有办法在这种情况下检索另一个对象(或某种回调)?
【问题讨论】:
-
会
null或某种 MyDeserializedClass 子类而不是MyDeserializedClass with null values为您修复它吗? -
为什么不返回字符串而使用jackson来映射呢?如果为 null 将很容易检测到。
-
@varren 如果我得到 null 而不是具有 null 值的对象,那么它会满足我
标签: json spring resttemplate json-deserialization