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:
[
{
"_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):
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. 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:
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 [m.m.restspringtest.Mall.Mall] and content type [application/json]
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. I provided wrong class, there should be:
RestTemplateJSON restTemplate = new RestTemplateJSON();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
Mall[] malls = restTemplate.getForObject(url, Mall[].class);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With