尝试反序列化 Jackson 中的响应时出错
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not construct instance of org.proj.model.profile.Profile, problem: Should never call 'set' on setterless property
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@2d881384; line: 10, column: 1] (through reference chain: java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.proj.model.profile.Profile, problem: Should never call 'set' on setterless property
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@2d881384; line: 10, column: 1] (through reference chain: java.util.ArrayList[0])
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:208)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:200)
此时,我在 Profile 构造函数中添加了另一个变量
@JsonProperty("childrens") List<String> childrens
调用后出现错误:
return restClient.exchange(PROFILES_URL + "/name" + path, HttpMethod.GET, restClient.makeHttpEntityWithAuth(), new ParameterizedTypeReference<Collection<Profile>>() {
});
我在网上搜索了错误的名称,但没有找到。
简介类:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Profile{
private final String id;
private final List<String> childrens;
public Profile(
@JsonProperty("id") String id,
@JsonProperty("childrens") List<String> childrens) {
this.id = id;
this.childrens = childrens;
}
public String getId() {
return id;
}
public List<String> getChildrens() {
return childrens;
}
有什么问题?
你忘记
@JsonCreator了构造函数。这就是杰克逊试图使用 Setter 而不是构造函数的原因,而您只是没有 setter。这是关于这个主题的更多内容