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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

mccarlIT 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mrayanduttaAuthor Commented:
Thanks for your help
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
No worries!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.