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

  • Custom Exceptions handling using RestTemplate

    Hi,

    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:

    Code:
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    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:

    Code:
    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: http://stackoverflow.com/questions/3...-of-no-content

    Comment


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

      Comment

      Working...
      X