Announcement Announcement Module
No announcement yet.
GsonHttpMessageConverter can't convert nested JSON response to JsonElement Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • GsonHttpMessageConverter can't convert nested JSON response to JsonElement

    I have a problem with converting my JSON response to a Gson's JsonElement. If I get a simple JSON like:
    date: "6:51:26",
    user: "huviretki",
    text: ":P"
    date: "2:45:11",
    user: "Mike",
    text: "ciekawy wątek: "bez the" :D"
    There is no problem and the JsonElement object is being filled with a proper JSON. But if I want to get a nested JSON like:
    {username: "blackMasoon", trainer: {lvl: "3", exp: "123"}}
    The app crashes with:
    java.lang.RuntimeException: Unable to start activity ComponentInfo{}: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [] and content type [text/html;charset=UTF-8]
    Does GsonHttpMessageConverter have problem with nested JSON messages? Here's the code:
    RestTemplate restTemplate = new RestTemplate();
    		restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());	
    		HttpHeaders requestHeaders = new HttpHeaders();
    		requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
    		HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
    		return restTemplate.getForObject(baseUrl+query,JsonElement.class, requestEntity);

  • #2
    The first thing I notice is the content type being returned is text/html. You can see this declared in the stack trace. You've registered the GsonHttpMessageConverter to accept application/json, so it won't be used to convert the response of another type. You can attempt to add the content type as the accept header, but I suspect there is something else going in with the response, because JSON is not typically returned with that content type.