Announcement Announcement Module
No announcement yet.
Custom Exceptions handling using RestTemplate Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom Exceptions handling using RestTemplate


    I want my RESTful webservices to throw Exceptions. On server I'm using CXF to do all RS-311 stuff, including catching my exceptions and putting exception data into HTTP headers (my way of doing this) and setting status to CONFLICT. No problem at all. But on client side, where I'm using RestTemplate, I don't see any way to first check if there was an Exception on server side, and if so gather all the data and throw the same Exception on client side. That is I'm looking for some generic method of doing so for all requests. I've found ResponseErrorHandler interface and I know how it is used, but handleError method assumes that I can only throw IOException. Theoretically I can put mine whole Exception hierarchy into IOException, but that's a bad idea.

    My idea is simple, if server responses with 409 (CONFLICT) and there are two HTTP headers RESTException and RESTExceptionMessage, then I want exception thrown from RestTemplate. Is there any generic way of doing so? Or maybe I should avoid throwing Exceptions from RESTful webservices?

    Best Regards

    Jacek Bilski

  • #2
    Hi Jacek,
    I am having the same issue.

    One the server side I defined some errors, which a relevant for my project. These errors are put into an object named ErrorType. A simplified example looks like this:

    public ModelAndView handleException(Exception ex, HttpServletRequest request) {
    	ModelAndView mov = new ModelAndView();
    	ErrorType errorType = ErrorTypeConverter.Convert(ex);
    	mov.addObject("errorType", errorType);
    	return mov;
    On the client side I am reading the data like this:

    try {
    	objectResponse = restTemplate.postForObject(location, objectType, ObjectType.class);
    } catch (HttpClientErrorException e) {
    	//** Here I want to read out the error type instead **//
    What I would need now, is an parser telling me which object to load. Did you come up with a solution yet? The most attractive way I found yet is here:


    • #3
      Anyone else here has an idea, or a proper solution how to solve it?