-
Notifications
You must be signed in to change notification settings - Fork 38.4k
Make it easier for RestTemplate to deal with error object responses [SPR-10961] #15589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Phil Webb commented Is it possible to obtains the error response directly from try {
DoodadResources response = restTemplate.whatever...
}
catch (MyErrorResourceException ex) {
ex.getCode();
ex.getWhateverFromTheErrorResponse();
} |
Willie Wheeler commented If you mean that the call ex.getWhateverFromTheErrorResponse() would handle the deserialization behind the scenes--e.g.
then yeah, that would be very convenient from an API user's perspective. I like it. |
Phil Webb commented Do the existing |
Willie Wheeler commented Well, the one thing though is that there's no standard error object format. So "ErrorObject" wouldn't be something that Spring provides--it would need to reflect the schema of the API that generated it (e.g. GitHub, HipChat, etc.) So I think that you end up still needing to pass a type param into the RestClient call to specify the return type for the error object. Now if RestTemplate wants to pass that along to HttpStatusCodeException so that HttpStatusCodeException can return an appropriately typed error object, superb. I do like the idea though of exposing the object through the exception itself. That makes a lot of sense to me. |
Willie Wheeler commented OK, I think I understand better your suggestion. You are saying that I implement ResponseErrorHandler and get the error body ClientHttpResponse.getStatusText(). Then I throw a custom exception that exposes the deserialized exception. That is definitely superior to my current always-deserialize-to-String approach, so I will start doing that. Thanks. Having said that, I think it would still be a useful enhancement to RestTemplate itself if people didn't have to do this. By now I think error objects are sufficiently common (GitHub and HipChat have them, for example) that it would be nice if RestTemplate just allowed the user to provide the error type as a type param (passed along to HttpStatusCodeException). Then HttpStatusCodeException itself would expose the error object. Pretty hands-free for a common requirement. Thanks. |
Phil Webb commented (sorry I was typing this before I saw your followups) I was thinking more that you would write a specific With a custom RestTemplate t = new RestTemplate();
t.setErrorHandler(new HipChatErrorHandler());
try {
t.exchan...
}
catch (NastyHipChatException ex) {
ex.somethingSpecificExtractedFromTheResponse()
}
catch (OtherNastyHipChatException ex) {
ex.somethingElseSpecificExtractedFromTheResponse()
} You could even create your own exception hierarchy as needed. BTW: Have you seen the Spring Social GitHub bindings? |
Phil Webb commented Just so I understand you mean something like this: try {
HipChatResponse r = restTemplate.postForObject(uri, request, HipChatResponse.class, HipChatError.class);
}
catch (HttpErrorResponseException ex) {
HipChatError error = (HipChatError) ex.getErrorObject();
} |
Willie Wheeler commented Yup Phil, that's right. Re: Spring Social GitHub: It's been a while though. :-) |
Phil Webb commented Given that current design will allow you to deal with this via |
Geoffrey Wiseman commented Handling this through an exception is a viable workaround, but it's clumsy, and not well-suited to handling a wide array of REST APIs (IMO) |
Rossen Stoyanchev commented Geoffrey Wiseman, in 5.0 we have added the WebClient which allows you to perform an As for the Note that in 5.0 we have added a dedicated |
Geoffrey Wiseman commented I admit, your hands are a little tied by the existing It does looks like |
Rossen Stoyanchev commented Yes in more than one respects |
Willie Wheeler opened SPR-10961 and commented
One challenge with RestTemplate is that it expects a single response type when making calls. Of course we get to choose it, but there's only one per call. The problem is that some APIs (e.g. Github API as one example) return either the expected type or else an error object, depending on whether there was an error.
It would be great if we could tell RestTemplate to deserialize into such-and-such class if there's no error, and into some other class if there is an error.
Right now the workaround I'm using is to deserialize into a String, and then parse it into the correct type depending on the status code. This is OK, but it's kind of a pain (see the blog post above), and worse, it requires buffering the entire response payload as a string, which may not be great if the payloads are large.
Affects: 4.0 M3
Reference URL: http://springinpractice.com/2013/10/07/handling-json-error-object-responses-with-springs-resttemplate/
Issue Links:
30 votes, 25 watchers
The text was updated successfully, but these errors were encountered: