Rest client with AndroidAnnotations - "there is no suitable HttpMessageConverter ..."


I want to send a POST request to the server. I have to pass the JSON object as a parameter and get the JSON as the response, but I get 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

Send 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 use 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 am I doing wrong?
When I change the send parameter to JSONObject , I get the same error.
Btw. AA docs are really cryptic - can I use Gson?
Or should I use Jackson?
Which file do I need to include?

Thanks for any help!

+4
source share
3 answers

You can use RestTemplate with Gson or Jackson.

Gson is fine and easier to use, you have a small json dataset. Jackson is more suitable if you have a complex / deep json tree, because Gson creates a lot of temporary objects, which causes the global GC to stop.

The error here suggests that he cannot find an HttpMessageConverter capable of parsing application/octet-stream .

If you look at the sources of the GsonHttpMessageConverter , you will notice that it only supports the mimetype application/json .

This means that you have two options:

  • Or return from your content that will match correctly
  • Or just change the supported media types to 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); 
+4
source

I had this problem. After a few hours, I realized that the class that I passed to the RestTemplate.postForObject call had Date variables. You must make sure that it contains only simple data types. Hope this helps someone else!

0
source

I need to modify it a bit to work:

 final List<MediaType> list = new ArrayList<>(); list.addAll(converter.getSupportedMediaTypes()); list.add(MediaType.APPLICATION_OCTET_STREAM); converter.setSupportedMediaTypes(list); 
0
source

Source: https://habr.com/ru/post/1433990/


All Articles