简体   繁体   中英

Spring RestTemplate can not convert json response

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]

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.

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"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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