Announcement Announcement Module
No announcement yet.
RestTemplate cannot read response (when http was 401) Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • RestTemplate cannot read response (when http was 401)

    RestTemplate was configured to convert a json response to an object, I mean, there is almost no configuration.

    Problem is, lets say the user is not authorized for some reason, and the webservice returns a http status 401.

    RestTemplate still tries to convert to the object and accuses that cant convert whatever (message converter not found) it came to the object.

    So, question is: how can I configure to not let it convert if the status is not OK ?

    Possible solution: for what I can see, I would have to add a new message converter and verify the code before converting, problem is that I would have to rewrite all default convertors?

    It seems that I found a good answer: that uses ResponseExtractor as an argument
    Last edited by lucas1223; Mar 17th, 2012, 01:53 PM.

  • #2
    So,the solution I used to fix the problem was to extend RestTemplate, creating my own, and to modified the class private class ResponseEntityResponseExtractor so it verifies the status before trying to extract.

    For me this is a no brainer, why would the extractor extract something if the response is not HTTP.OK?

    So, is this a bug?

    Another question, is there a way to set the ResponseEntityResponseExtractor in another way? To have to extend the class to modify that code shouldnt be necessary I suppose, maybe I am doing something wrong and there is a better way.

    Another note: based on the response @stackoverflow, according to SPR-8016, there was an issue and the problem was fixed, but, it looks like the fix wasnt applied to the android class @spring-android-rest-template, my version here @1.0.0.RC1
    Last edited by lucas1223; Mar 17th, 2012, 03:00 PM.


    • #3
      SPR-7911 was merged into Spring for Android in RC1 (ANDROID-57), which checks for a 204 or 304 response, and content length of 0 before trying to extract the response. SPR-8016 is waiting on triage, but it appears to be a duplicate of SPR-7911. To answer your question, you should not have to modify the code to handle this case. I've created a new issue to look into it (ANDROID-80). Thanks for your feedback!


      • #4
        Are you using the SimpleClientHttpRequestFactory? After investigating this issue [1], I uncovered a difference in behavior with HttpURLConnection when receiving an HTTP 401 response. HttpURLConnection throws an IOException. I've opened a new JIRA for this [2], and it may be the root cause for what you are seeing.