4


I want to send POST request to server. I have to pass JSON object as a parameter, and get JSON as a response, but I am getting this error:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.package.Response] and content type [application/octet-stream]

Code

Sending request:

    @RestService
    RestClient restClient;
...
    String json = "{\"param\":3}"; 
    restClient.getRestTemplate().getMessageConverters().add(new GsonHttpMessageConverter());
    Response res = restClient.send(json);

RestClient

@Rest("http://my-url.com")

    public interface RestClient
    {
        @Post("/something/")
        Response send(String json);

        RestTemplate getRestTemplate();

        void setRestTemplate(RestTemplate restTemplate);
    }

I'm using these JAR files:

  • spring-android-rest-template-1.0.0.RC1
  • spring-android-core-1.0.0.RC1
  • spring-android-auth-1.0.0.RC1
  • gson-2.2.2

What I'm doing wrong?
When I change send parameter to JSONObject I am getting the same error.
Btw. AA docs are really enigmatic - can I use Gson anyway?
Or should I use Jackson?
Which file do I need to include then?

Thanks for any help!

Piotr
  • 1,743
  • 1
  • 26
  • 40

3 Answers3

4

You can use RestTemplate with either Gson or Jackson.

Gson is fine and easier to use of you have small json data set. Jackson is more suitable if you have a complex / deep json tree, because Gson creates a lot of temporary objects which leads to stop the world GCs.

The error here says that it cannot find a HttpMessageConverter able to parse application/octet-stream.

If you look at the sources for GsonHttpMessageConverter, you'll notice that it only supports the mimetype application/json.

This means you have two options :

  • Either return the application/json mimetype from your content, which would seam quite appropriate
  • Or just change the supported media types on GsonHttpMessageConverter :
String json = "{\"param\":3}"; 
GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
converter.setSupportedMediaTypes(new MediaType("application", "octet-stream", Charset.forName("UTF-8")));
restClient.getRestTemplate().getMessageConverters().add(converter);
Response res = restClient.send(json);
Pierre-Yves Ricau
  • 8,209
  • 2
  • 27
  • 43
  • Thanks you very much! Will new version of AA support annotations for HttpMessageConverters? – Piotr Sep 14 '12 at 19:01
  • 1
    Yep, that's part of the plan :) – Pierre-Yves Ricau Sep 25 '12 at 15:38
  • Cool, thanks Piwaï. Just a bit more info in case others need it: to add this, I ditched the auto-generated-from-interface RestClient and used the generated class as the basis - then added Piwaï's code above to the constructor. Except for me, setSupportedMediaTypes takes a List of MediaTypes so I added the MediaType to a List first. You can then switch \@RestClient for \@EBean and reference your class with \@Bean. Worked for me anyway! :) – poshaughnessy Jan 04 '13 at 23:27
0

I just had this problem. After several hours I realised that the class I was passing in to the RestTemplate.postForObject call had Date variables. You need to make sure it only contains simple data types. Hope this helps someone else!

Rob
  • 1
0

I have to modify it little to work:

final List<MediaType> list = new ArrayList<>();
list.addAll(converter.getSupportedMediaTypes());
list.add(MediaType.APPLICATION_OCTET_STREAM);
converter.setSupportedMediaTypes(list);
Ivan
  • 67
  • 4