• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Handling error in Spring MVC  RSS feed

 
Ranch Hand
Posts: 32
  • 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: 21503
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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!