我使用Spring Web创建休息资源。我用Java创建了类似于Json响应的Document对象。当我使用String.class作为响应类型时,我从请求中获得响应。当我将响应更改为我创建的类时,该男孩为空。
这是我的HttpGettter
private <T> T perform(String token, String resourcePath, Class<T> responseType, HttpMethod method, MultiValueMap<String, String> queryParams) {
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.newInstance()
.scheme(protocol)
.host(host);
UriComponents uriComponents = uriComponentsBuilder.build();
String url = uriComponents.toString();
HttpHeaders headers = new HttpHeaders();
headers.add("contentType","application/json");
HttpEntity<String> requestEntity = new HttpEntity<>(headers);
ResponseEntity<T> responseEntity = restTemplate.exchange(url, method, requestEntity, responseType);
return responseEntity.getBody();
}
return null;
}
Json文件
@JsonIgnoreProperties(ignoreUnknown = false)
public class SiteDocument {
@JsonProperty("id")
private int siteID;
@JsonProperty("site_name")
private String siteName;
@JsonProperty("url")
private String url;
}
函数调用
perform(null, "/sites", SiteDocument[].class, HTTP.get,null)
JSON响应
{
"items":[
{
"id":5841970702,
"site_name":"test",
"url":"http://sample/again/",
"pages":2,
"policies":0,
"product":[
"quality_assurance",
"accessibility",
"seo",
"policy"
],
"_links":{
"site":{
"href":"https://api.siteimprove.com/v2/sites/5841970702"
}
}
},
{
"id":5881398958,
"site_name":"preview.site",
"url":"http://sample.site",
"pages":0,
"policies":0,
"product":[
"quality_assurance",
"policy"
],
"_links":{
"site":{
"href":"https://api.siteimprove.com/v2/sites/5881398958"
}
}
}
],
"total_items":5,
"total_pages":1,
"links":{
"self":{
"href":"https://api.siteimprove.com/v2/sites"
}
}
}
忽略JSON响应的错误格式。编辑简短。
异常
线程中的异常&#34; main&#34; org.springframework.web.client.RestClientException:无法解压缩 响应:没有为响应类型找到合适的HttpMessageConverter [类 [Lcom.blueprint.siteimprove.webresources.documents.SiteDocument;] 和内容类型[text / html]
更新了例外
无法提取回复:找不到合适的HttpMessageConverter 响应类型[类 [Lcom.coremedia.blueprint.siteimprove.webresources.documents.SiteDocument;] 和内容类型[application / json; charset = utf-8]
在标题上添加了愚蠢的
headers.add("Accept","application/json");
headers.add("Content-Type","application/json");
答案 0 :(得分:0)
问题出在POM文件中。 POM文件中未添加运行时依赖项。要在POM文件中添加以下依赖项。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>runtime</scope>
</dependency>