Spring Rest Template exception handling

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
mrayanduttaAsked:
Who is Participating?
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
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

0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
mrayanduttaAuthor 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
0
 
mrayanduttaAuthor Commented:
Thanks for your help
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
No worries!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.