Exception Handling in Spring
Hey i am getting 500 response code instead of 404 even after handling the exception
14 Replies
⌛
This post has been reserved for your question.
Hey @Danix! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
Try add the
@ResponseStatus(HttpStatus.NOT_FOUND)
on the exception-class.already did it but no changement after that
Ah ok. What spring version? 4.2?
This one
Ok. Then I need more informations. Please show the stacktrace (obfuscate things you need to hide).
You should return an empty optional if resource is not found and handle it in the controller.
Although this code should work there's no reason to add piles of magic.
Hmm, your service handles the business logic, so if you expect to get a value from the database there and you don't have it, that's the place to throw an exception, not in the controller. The controller should handle any unexpected exceptions that come up though, because that's it's task
Doesn't sound like a case for exceptions. Missing value isn't anything exceptional.
Although this whole code is a mess. Somehow it returns a list that is packed in Optional...
It is if you actually expect it to be there. But I agree on the second point
Are you sure? Can you prove it by show the code?
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.