[英]No suitable HttpMessageConverter found for response type - Custom RestTemplate
First, sorry for possible duplicate. 首先,抱歉可能重复。 I found some questions regarding to similar problems.
我发现了一些有关类似问题的问题。 However, I still can't figure out what's wrong in my specific case.
但是,我仍然无法弄清楚我的具体情况有什么问题。
So, example json from server: 所以,来自服务器的示例json:
[
{
"_id": "55f9690f30ef6f210e2dc3a5",
"ID": "74c4bf82-9f78-4df5-b9d7-6547e2a55eaa",
"Name": "myLand, Saarbrücken",
"__v": 0,
"Shops": [
{
"ID": "b8eacee1-b2c6-48aa-ac6f-2e7fbe3a5d68",
"Name": "ARA",
"_id": "55f9690f30ef6f210e2dc3a6",
"News": [
{
"ID": "d79b7f51-7d5c-4bd6-9321-e40c6e93788c",
"ValidFrom": "2015-01-08T00:00:00",
"ValidTo": "2015-09-30T00:00:00",
"_id": "55f9690f30ef6f210e2dc3a7",
"Texts": [
{
"ID": "TITLE",
"Value": "11. Wochenspiegel Firmenlauf",
"_id": "55f9690f30ef6f210e2dc3a9"
},
{
"ID": "BODY",
"Value": "Wir gratulieren zur ersten und gleich sehr erfolgreichen Teilnahme am 11.Wochenspiegel Firmenlauf in Dillingen,\r\nunsere Teams vom “Outlet center Wadgassen“ haben ihren Lauf mit tollen Zeiten abgeschlossen und hatten trotz\r\nhohen Temperaturen einen wunderbaren Tag – wie man sehen kann. Wir freuen uns schon jetzt auf nächstes Jahr!",
"_id": "55f9690f30ef6f210e2dc3a8"
}
]
}
],
"Texts": [
{
"ID": "DESCRIPTION",
"Value": "Mit Tradition in die Zukunft Seit sechs Jahrzehnten steht ara für vielfältige Schuhmode erstklassiger Qualität,",
"_id": "55f9690f30ef6f210e2dc3aa"
}
]
}
]
}
]
I generated class called Mall (and all subclasses for the rest of data structure): 我生成了一个名为Mall的类(以及其余数据结构的所有子类):
public class Mall
{
private String Id;
private String ID;
private String Name;
private int V;
private List<Shop> Shops = new ArrayList<Shop>();
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
/**
* @return The Id
*/
public String getId()
{
return Id;
}
/**
* @param Id The _id
*/
public void setId(String Id)
{
this.Id = Id;
}
/**
* @return The ID
*/
public String getID()
{
return ID;
}
/**
* @param ID The ID
*/
public void setID(String ID)
{
this.ID = ID;
}
/**
* @return The Name
*/
public String getName()
{
return Name;
}
/**
* @param Name The Name
*/
public void setName(String Name)
{
this.Name = Name;
}
/**
* @return The V
*/
public int getV()
{
return V;
}
/**
* @param V The __v
*/
public void setV(int V)
{
this.V = V;
}
/**
* @return The Shops
*/
public List<Shop> getShops()
{
return Shops;
}
/**
* @param Shops The Shops
*/
public void setShops(List<Shop> Shops)
{
this.Shops = Shops;
}
public Map<String, Object> getAdditionalProperties()
{
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value)
{
this.additionalProperties.put(name, value);
}
}
Server returns conent-type text/plain. 服务器返回conent-type text / plain。 To modify content type, I wrote simple extend class:
要修改内容类型,我编写了简单的扩展类:
public class RestTemplateJSON extends RestTemplate
{
@Override
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor) throws RestClientException
{
//logger.info(RestTemplateJSON.class.getSuperclass().getSimpleName() + ".doExecute() is overridden");
Assert.notNull(url, "'url' must not be null");
Assert.notNull(method, "'method' must not be null");
ClientHttpResponse response = null;
try
{
ClientHttpRequest request = createRequest(url, method);
if (requestCallback != null)
{
requestCallback.doWithRequest(request);
}
response = request.execute();
// Set ContentType to JSON
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
if (!getErrorHandler().hasError(response))
{
logResponseStatus(method, url, response);
} else
{
handleResponseError(method, url, response);
}
if (responseExtractor != null)
{
return responseExtractor.extractData(response);
} else
{
return null;
}
}
catch (IOException ex)
{
throw new ResourceAccessException("I/O error on " + method.name() +
" request for \"" + url + "\":" + ex.getMessage(), ex);
}
finally
{
if (response != null)
{
response.close();
}
}
}
private void logResponseStatus(HttpMethod method, URI url, ClientHttpResponse response)
{
//if (logger.isDebugEnabled())
{
try
{
System.out.println(method.name() + " request for \"" + url + "\" resulted in " +
response.getRawStatusCode() + " (" + response.getStatusText() + ")");
}
catch (IOException e)
{
// ignore
}
}
}
private void handleResponseError(HttpMethod method, URI url, ClientHttpResponse response) throws IOException
{
getErrorHandler().handleError(response);
}
}
Finally, this is how I'm trying to consume my webservice: 最后,这就是我尝试使用我的web服务的方式:
RestTemplateJSON restTemplate = new RestTemplateJSON();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
Mall mall = restTemplate.getForObject(url, Mall.class);
However, I'm still getting the same exception: 但是,我仍然得到同样的例外:
Could not extract response: no suitable HttpMessageConverter found for response type [mmrestspringtest.Mall.Mall] and content type [application/json]
无法提取响应:没有为响应类型[mmrestspringtest.Mall.Mall]和内容类型[application / json]找到合适的HttpMessageConverter
As far as I know, if I change content type, it should be ok. 据我所知,如果我更改内容类型,它应该没问题。 Can you please tell me what am I doing wrong?
你能告诉我我做错了什么吗?
So, I finally figured it out. 所以,我终于明白了。 There is array in json, so I want to map it to Mall[], not Mall.
在json中有数组,所以我想将它映射到Mall [],而不是Mall。 I provided wrong class, there should be:
我提供了错误的课程,应该有:
RestTemplateJSON restTemplate = new RestTemplateJSON();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
Mall[] malls = restTemplate.getForObject(url, Mall[].class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.