0

By using

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

and

org.springframework.validation.annotation.Validated on @RequestBody and involved POJO fields, spring (MVC) will help validate incoming request body automatically, and throw exception when the request body is invalid.

And by using @ExceptionHandler(value = MethodArgumentNotValidException.class), I will capture the exception and then return a more readable response body to frontend.

But by this way, I cannot get the whole request body. Is it possible to get the whole request body? Could you please help advise?

Thanks a lot.

ildvzg68472
  • 157
  • 1
  • 9
  • Is there any reason why you want to get the whole request body? if you really need it then you can try to defer the validation process so that you can process it first then run the validation manually – Aleson Sep 22 '22 at 05:10
  • @Aleson Because yesterday, frontend passed a correct request body, but because I set a wrong value in `@JsonProperty`, so the `@NotNull` field reported not-null exception. If I can see the request body, then I will know it is my fault at once. But now I have to ask for the request body from frontend team. Could you please share the best practice about "process it first then run the validation manually"? Thanks a lot. – ildvzg68472 Sep 22 '22 at 05:35
  • 1
    As the provider of an API you don't have to send much detailed messages or detailed exceptions. You should instead log your errors with more details and just send a simple message in the API. – Harry Coder Sep 22 '22 at 07:02
  • I see so you need the whole request body for debugging processes, here is some reference links which I mention before about "process it first then run the validation manually" https://stackoverflow.com/questions/19190592/manually-call-spring-annotation-validation https://reflectoring.io/bean-validation-with-spring-boot/ – Aleson Sep 22 '22 at 07:41

0 Answers0