Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Handling error in Spring MVC  RSS feed

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've my Spring MVC controller defined in the following manner right now without much error handling mechanism :



I decided to add some error handling like the following :



Could anyone suggest if this is a correct way to handle the error OR should include the try-catch block in the code above? Also, if there's an database or any other hibernate error, I think, it would make sense to display on this line
How can I do that?


For reference (if needed), I have my other classes defined  in the thread that I created for this question :

https://coderanch.com/t/697432/java/Printing-JSON-Spring-MVC-hibernate  ;

Thanks!
 
Sheriff
Posts: 21421
94
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's bad design to use 200 responses for error responses. If something goes wrong, the status code should reflect this: 400 if there is something wrong with the request, 500 if something goes wrong while saving. You can use exceptions and an @ExceptionHandler for this; see https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc for more information.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!