Spring Rest Template exception handling

mrayandutta
mrayandutta used Ask the Experts™
on
Hi ,
I am using Spring Rest Template for invoking a web service .
I want to handle all types of exceptions returned by Rest Template .
Any body has some proper links/sample code describing the exception handling scenario using Spring Rest Template ?
Regards,
Ayan
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
There really isn't anything too special about it, this link may help, http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/remoting.html#rest-resttemplate, but you may have already come across it.

If the above doesn't help, can you please elaborate more on what you are trying to do, any issues that you are coming across, etc something more specific for us to provide help on?
Top Expert 2013

Commented:

Author

Commented:
Hi ,
Thanks to both you for your replies .

@mccarl

In my application there is an Ajax call to a spring controller .
This controller calls RestTemplate and in turn returns the output received from the ResTemplate as the Ajax response .

For any exception occurring  at the REST Template end, the controller needs to pass the response code only as the Ajax response .
So if there is any exception in the RestTemplate invocation ,I need to get the response code of the rest template (say 404 or 500 ) and  the controller would return the error code to its invoker .

But as the exceptions of Rest Template seems to be unchecked ,this task seems bit complicated .
I hope that explains my scenario a bit .

Regards,
Ayan
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
But as the exceptions of Rest Template seems to be unchecked ,this task seems bit complicated .
There is nothing special about unchecked exceptions other than the fact that you don't NEED to catch them. However, if you do want to catch them, then you do what you normally would do.

Now also looking at the Exception Hierarchy for the RestClientException (what the RestTemplate will throw) it looks fairly easy to do what you what. It should just be something like...
RestTemplate restTemplate = ......; // However you get/init your template

...

// in your controller handler method...
try {
    obj = restTemplate.getForObject(.....);
} catch (HttpStatusCodeException ex) {
    int statusCode = ex.getStatusCode().value();
    // return this statuscode in the ajax response
} catch (RestClientException ex) {
    // Some other error occured, not related to an actual status code
    // return whatever you need to in this case, maybe another 5xx code.
}
// Return the normal ajax response, obtained from the "obj" rest response above
...

Open in new window

Author

Commented:
Thanks for your help
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
No worries!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial