javaandroidspringrobospicespring-android

Spring for Android - Could not extract response: no suitable HttpMessageConverter found for response type


I'm trying to retrieve some data from a REST service using spring for Android. However I'm running into problems. I'm also using Robospice - and as such have a method like so:

 public FunkyThingsList loadDataFromNetwork() throws Exception {
        String baseURI =context.getResources().getString(R.string.baseWebServiceURI);
                String uri = baseURI + "/FunkyThings/date/" + dateToLookUp.toString("yyyy-MM-dd");
        RestTemplate restTemplate = getRestTemplate();
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.setAccept( Arrays.asList(MediaType.APPLICATION_JSON));
        HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
        headers.setAuthorization(authHeader);

        // Create the request body as a MultiValueMap
        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
        HttpMessageConverter<String> stringConverter = new StringHttpMessageConverter();
        FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
        List<HttpMessageConverter<?>> msgConverters = restTemplate.getMessageConverters();
        msgConverters.add(formConverter);
        msgConverters.add(stringConverter);
        restTemplate.setMessageConverters(msgConverters);

        HttpEntity<?> httpEntity = new HttpEntity<Object>(map, headers);
        final ResponseEntity<FunkyThingsList> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity,FunkyThingsList.class);

        return responseEntity.getBody();
    }

Unfortunately this isn't working. I'm getting the following exception thrown:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.MyProject.DataClassPojos.RoboSpiceArrayLists.FunkyThingsList] and content type [application/json;charset=utf-8]

Now based on my Googling I get the feeling I need to add a message converter. I'm just not sure which message converter I need, or where I add it?


Solution

  • For the default JSON HttpMessageConverter, you'll need to add either Jackson 1 or Jackson 2 to your classpath.

    Otherwise, you can add some other JSON library and write your own HttpMessageConverter which can do the deserialization. You add it to the RestTemplate. You can either use the constructor or this method.