Rest error handler and HttpMessageNotReadableException

1,569 views
Skip to first unread message

Thomas Bruyelle

unread,
May 12, 2014, 5:49:34 AM5/12/14
to androidan...@googlegroups.com
The RestErrorHandler interface allows to catch RestClientException, but it seems it doesn't catch all the kinds of exception that can be thrown by the RestTemplate. 

In my case, when the mobile connection is lost, I can get a HttpMessageNotReadableException : 

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: recvfrom failed: ETIMEDOUT (Connection timed
                           
out); nested exception is java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)
                        E   at org
.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:1
                           
25)
                        E   at org
.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:147)
                        E   at org
.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:76)
                        E   at org
.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:655)
                        E   at org
.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:641)
                        E   at org
.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:484)
                        E   at org
.springframework.web.client.RestTemplate.execute(RestTemplate.java:447)
                        E   at org
.springframework.web.client.RestTemplate.exchange(RestTemplate.java:422)


The exception is not catched and so my app crashes miserably. 
How should I handle such case ? Put catch everywhere ? I would except something more elegant.

Thanks in advance

Damien Villeneuve

unread,
May 13, 2014, 2:55:37 AM5/13/14
to androidan...@googlegroups.com
Damn you're right, we should catch all exceptions, not only RestClientException.

The easiest way is to create a bean in which you'll inject the RestClient and provide the same methods signature as in the Restclient. The only thing these methods will do is to call the related RestClient's method in a try catch(Exception).

So you'll come to this :

@Rest("...")
public interface RestClient {
  @Get("...")
  MyDTO myWS();
}

@EBean
public class RestClientProxy  {
  @RestService
  RestClient restClient

  public MyDTO myWS() {
    try {
      return restClient.myDTO;
    } catch (Exception e) {
      // do something

Damien Villeneuve

unread,
May 13, 2014, 3:54:51 AM5/13/14
to androidan...@googlegroups.com
Could you open an issue about this on github ?

Thomas Bruyelle

unread,
May 14, 2014, 6:06:23 AM5/14/14
to androidan...@googlegroups.com


The easiest way is to create a bean in which you'll inject the RestClient and provide the same methods signature as in the Restclient. The only thing these methods will do is to call the related RestClient's method in a try catch(Exception).



Yes, that's what I'm doing. 

Thomas Bruyelle

unread,
May 14, 2014, 6:14:51 AM5/14/14
to androidan...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages