Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Handling error in Spring MVC  RSS feed

 
Ranch Hand
Posts: 69
  • 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: 21602
101
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.
 
On top of spaghetti all covered in cheese, there was this tiny ad:
Enterprise Java integration with Delphi and Free Pascal applications - new release 2019.02
https://coderanch.com/t/705708/Enterprise-Java-integration-Delphi-Free
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!