Spring RestTemplate can not convert json response

你。 提交于 2019-12-11 17:32:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!