问题
I have url - http://hzhzhz
return json
{
"someField": 3,
"datesField": ["2017-08-19",
"2017-08-20",
"2017-08-26",
"2018-12-30"]
}
I create models
@Data
@NoArgsConstructor
private class Response{
private int someField;
private DatesField datesField;
}
@Data
@NoArgsConstructor
private class DatesField{
private List<String> strings;
}
I create
ResponseforObject = restTemplate.getForObject("http://hzhzhz", Response.class);
Amd I get error:
Could not extract response: no suitable HttpMessageConverter found for response type [class mypackeg.Response] and content type [text/html;charset=utf-8]
回答1:
Your "http://hzhzhz"
call returns HTML which cannot be converted to the mypackeg.Response class.
Could be URL is wrong or it produces wrong content type (HTML instead of expected JSON or XML). To fix try to return String.class and check what exactly in the string.
One more possible reason is permission denied which returns access denied HTML page.
回答2:
Change the http://hzhzhz endpoint to return
content type = 'application/json'
If the http://hzhzhz is made by spring use this in the request mapping
produces = "application/json"
回答3:
If you use spring controller, you should change produces as "application/json"
来源:https://stackoverflow.com/questions/45758437/spring-resttemplate-can-not-convert-json-response