3

I am coming from a .NET background in developing REST APIs. Now working on java REST project using spring boot.

First my validation at the controller @RequestBody just stop working. While trying to fix it, I saw different ways to implement. So what would be the correct way to annotate the @RequestBody?

@Validated @RequestBody

or

@Valid @RequestBody
alltej
  • 6,787
  • 10
  • 46
  • 87

2 Answers2

8

There are generally no huge difference between them two, @Valid is from JSR-303 standand, @Validated is spring standard. according to spring document:

Spring provides a Validator interface that can be used for validation in all layers of an application. In Spring MVC you can configure it for use as a global Validator instance, to be used whenever an @Valid or @Validated controller method argument is encountered, and/or as a local Validator within a controller through an @InitBinder method. Global and local validator instances can be combined to provide composite validation.

Reference: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-validation

However there are differences, one of them is for example, if you need use group in your validation annotation, you need to use @Validated, it is not supported with @Valid.

Jaiwo99
  • 9,687
  • 3
  • 36
  • 53
0

@Valid is in the JSR-Specification and @Validated is from spring framework.

When your program should be compatible to EJB/JSR Standard use @Valid otherwise you can use both.

Matthias
  • 1,378
  • 10
  • 23