how to use validation in my controller?
hey guys. im looking for advice on this question. right now i have this:
i need to manually check if some fields of my DTO are null. this is my
myValidationService
's method:
so my questions are:
1. should i craete try-catch
block in my controller, and in try
block call the validation method?
2. or can i leave the controller as is?
3. if amount or clientId is null, i want to return 400 bad request. do i need to create a custom exception that should be handled in my exception handler?
thanks for your help4 Replies
⌛
This post has been reserved for your question.
Hey @Fragmented friends! 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 marked as dormant after 300 minutes of inactivity.
you can use spring validation and annotations to each field in your dto
like @NonNull
but then you need a separate class for the data for request body
so paymentdto and paymentrequestbodydto or something
and then it will throw a validation error when a field is null or some other case
and you can use rest controller advice to capture
Thanks!
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
💤
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.