How to apply request body validation with error details?
Hello there,
I want to apply request body validation to my Spring project
and this endpoint
I thought
@Valid
would handle it for me but actually the API consumer still gets a 400 without any error details. So the consumer doesn't know what's wrong.
After googling around I think Spring doesn't do that for me out of the box. So I created an additional class
Unfortunately the result is the same, the API consumer won't get any error details. Is something missing? Do I even need the ValidationExceptionHandler
? How do I achieve this?
Thanks! π25 Replies
β
This post has been reserved for your question.
Hey @Garzec! 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 create a class that sums up all the validation errors and return it to the consumer
so you get something like
"error_message": "ID is required."
which is basically something that ur trying to do
also try using @RestControllerAdvice
would you mind telling me how to do this?
ValidationExceptionHandler
is wrong?https://www.bezkoder.com/spring-boot-restcontrolleradvice/
you can check here how they do it
BezKoder
@RestControllerAdvice example in Spring Boot - BezKoder
In this tutorial, weβre gonna look at an Spring Boot example that uses @RestControllerAdvice for exception handling in Restful API. I also show you the comparison between @RestControllerAdvice and @ControllerAdvice along with the use of @ExceptionHandler annotation. Related Posts: β @RestController vs @Controller β Spring Boot, Spring Data JPA β...
and no its not wrong
from that site
and that class error message will be returned to the client
but yea first swap to rest controller advice
@ayylmao123xdd thank you. I tried to follow the blog post and just changed from
@ControllerAdvice
to @RestControllerAdvice
since ErrorMessage
is just a custom type, I want to make use of problem details later on
Still, doesn't work π¦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.
yes you need to create a custom class
and it will return that class when an error happens
you mean like so?
That still doesn't work π¦
so what happens
when the id is invalid
nothing, when I set a breakpoint, it won't run the function
but what happens when the id is invalid
does it return the full stacktrace
you only get a 400 with
and you want the
"ID is required"
right
yes please
you can do something like this
basically
in that method argument not valid exception
you run the get field errors
and just add it to a lsit
sorry, the function
handleValidationException
never runs
if /foo
is valid, it executes the rest handler, if it's invalid then Spring responds with a 400, so handleValidationException
never runsbut it returns this
change the response status to maybe
not found
yes, the default Spring behavior for invalid request bodies I guess
and see if that changes anything
set to this
and see if it changes the error code
should be 404 instead of 400
no, same for
INTERNAL_SERVER_ERROR
Is there some Spring magic required? Do I have to place this exception handler file into a specific directory with a conventional name or so...hmmmmmmmm
can you try doing something like this
errorhandler is a separate class
also make sure you are using the correct notblank annotation
from jakarta.validation.constraints
yes, I think I got this ( as shown in my code above )
ok so try with the example i sent
π€
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.