1

I have an endpoint with a @Valid @RequestBody MyPojo pojo argument. When I call the endpoint and trigger a javax.validation failure, the request properly returns with a status 400. However, the response body is empty.

I can add a org.springframework.validation.Errors argument to my endpoint, handle the validation errors myself, and return them to the user. But can I get Spring to just do that out of the box? Why do I need to implement part of its built in validation manually?

Jorn
  • 20,612
  • 18
  • 79
  • 126
  • there should be reponse body with status, error, errors, timestamp – GolamMazid Sajib Apr 06 '20 at 10:08
  • What validation are you using in `MyPojo` class? – user1234SI. Apr 06 '20 at 10:34
  • @SebastianI. Stuff like `javax.validation.NotNull` and `javax.validation.Size`. And they work just fine, I can see that when using the `Errors` parameter based approach. – Jorn Apr 06 '20 at 10:45
  • @GolamMazidsajib I agree, that would be nice. Can you suggest something I can try to find out why there isn't? I get that response body just fine if I simply throw `ResponseStatusException` in other endpoints. – Jorn Apr 06 '20 at 10:48
  • Where are you testing the endpoints? Spring boot should have a response body in case that an exception is thrown. – user1234SI. Apr 06 '20 at 11:14
  • And anyway, it's better to handle the errors by yourself, in this way you have more control over the response body and throw sepcific http codes, messages etc. But right now, I can't see any reason why the response body is empty. – user1234SI. Apr 06 '20 at 11:17

1 Answers1

-1

You can use message input in your JSR 380 annotations like :

@NotNull(message = "Name cannot be null")
    private String name;

More: https://www.baeldung.com/javax-validation

Mammad
  • 99
  • 6
  • You are misunderstanding the question. Have a look at the comments. – Jorn Apr 06 '20 at 11:09
  • If you want some error response from validation, that's the way to do it, if you have issue with your response body, please post the code and more info so we can try to investigate. Thanks – Mammad Apr 06 '20 at 11:34