Spring RestTemplate 无法转换 json 响应 [英] Spring RestTemplate can not convert json response

查看:198
本文介绍了Spring RestTemplate 无法转换 json 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有网址 - 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);

我得到错误:

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

推荐答案

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

Your "http://hzhzhz" call returns HTML which cannot be converted to the mypackeg.Response class.

可能是 URL 错误或产生错误的内容类型(HTML 而不是预期的 JSON 或 XML).要修复尝试返回 String.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.

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

One more possible reason is permission denied which returns access denied HTML page.

这篇关于Spring RestTemplate 无法转换 json 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆