简体   繁体   English

Spring RestTemplate无法转换JSON响应

[英]Spring RestTemplate can not convert json response

I have url - http://hzhzhz 我有网址http://hzhzhz

return json 返回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]

Your "http://hzhzhz" call returns HTML which cannot be converted to the mypackeg.Response class. 您的"http://hzhzhz"调用返回的HTML无法转换为mypackeg.Response类。

Could be URL is wrong or it produces wrong content type (HTML instead of expected JSON or XML). 可能是网址错误或生成了错误的内容类型(HTML而不是预期的JSON或XML)。 To fix try to return String.class and check what exactly in the string. 要解决此问题,请尝试返回String.class并检查字符串中的内容。

One more possible reason is permission denied which returns access denied HTML page. 另一个可能的原因是拒绝权限,它返回拒绝访问的HTML页面。

Change the http://hzhzhz endpoint to return 更改http:// hzhzhz端点以返回

content type = 'application/json'

If the http://hzhzhz is made by spring use this in the request mapping 如果http:// hzhzhz是春季制作的,请在请求映射中使用它

produces = "application/json"

如果您使用spring控制器,则应将产生的内容更改为“ application / json”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM