Spring RestTemplate无法转换json响应

时间:2017-08-18 13:55:07

标签: java spring resttemplate

我有网址 - http://hzhzhz

返回json

{
    "someField": 3,
    "datesField": ["2017-08-19",
    "2017-08-20",
    "2017-08-26",
    "2018-12-30"]
}

我创建模型

@Data
@NoArgsConstructor
private class Response{
    private int someField;
    private DatesField datesField;
}

@Data
@NoArgsConstructor
private class DatesField{
    private List<String> strings;
}

我创建

ResponseforObject = restTemplate.getForObject("http://hzhzhz", Response.class);

Amd我收到错误:

Could not extract response: no suitable HttpMessageConverter found for response type [class mypackeg.Response] and content type [text/html;charset=utf-8]

3 个答案:

答案 0 :(得分:1)

您的"http://hzhzhz"调用返回无法转换为mypackeg.Response类的HTML。

可能是URL错误或产生错误的内容类型(HTML而不是预期的JSON或XML)。修复try以返回String.class并检查字符串中的确切内容。

另一个可能的原因是权限被拒绝,返回访问被拒绝的HTML页面。

答案 1 :(得分:0)

http://hzhzhz端点更改为

System.InvalidCastException: Specified cast is not valid.
   at System.Data.SqlClient.SqlBuffer.get_Int32()

如果http://hzhzhz是由spring制作的,请在请求映射中使用它

content type = 'application/json'

答案 2 :(得分:0)

如果使用弹簧控制器,则应将产品更改为“application / json”