user5620472
user5620472

Reputation: 2892

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]

Upvotes: 0

Views: 1992

Answers (3)

cengiz
cengiz

Reputation: 101

If you use spring controller, you should change produces as "application/json"

Upvotes: 0

Periklis Douvitsas
Periklis Douvitsas

Reputation: 2491

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"

Upvotes: 0

StanislavL
StanislavL

Reputation: 57421

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.

Upvotes: 1

Related Questions