Announcement Announcement Module
No announcement yet.
How to handle errors when using @ResponseBody in Spring MVC Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to handle errors when using @ResponseBody in Spring MVC

    I am using @ResponseBody for methods in controller. I am also using a HandlerInterceptorAdapter that calls database commit() in postHandle. If commit fails, I want the end user to know but by that time, it seems from the error message, the response has already been written. I am wondering how I should structure my code to achieve what I want. Of course, I don't want to write commit() in every controller method.


  • #2

    Ideally the database commits should happen from within your business layer and the web layer. You may want to restructure your code such that controller will delegate this task to a business layer which in turn will use the data access tier.

    The postHandle method also gets a ModelAndView parameter, you can populate your error message in it.

    Prasad Khandekar
    Last edited by KPrasadKhan; Apr 26th, 2012, 03:51 PM.