tlavarea
tlavarea

Reputation: 613

Spring Hibernate Validation @RequestBody

Using Spring Boot 1.4 and Spring Data Rest/MVC I'm unable to get @Valid @RequestBody working. I have tried multiple versions of Hibernate Validator, declaring Validtor beans etc. No luck.

@BasePathAwareController
@RestController
public class TestRestController extends BaseController {
    @PostMapping("/pojo/save")
    public @ResponseBody ResponseEntity<?> upload(@Valid @RequestBody MyPojo pojo) {
     // Code here calling a repository.save. 
}

MyPojo contains various validation annotations i.e. @Digits, @NotNull, etc. I actually see the validator exception when the repostiory.save method is called but Spring is not attempting any validation on the pojo.

Here is the section of my POM including the spring dependencies:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

Any ideas would be appreciated. I've done a ton of searching and tried many different configurations without any luck.

Update

When attempting to save the entity to the database, that's when the validation errors occur. I receive the following error:

Caused by: javax.persistence.RollbackException: Error while committing the transaction
Caused by: javax.validation.ConstraintViolationException: Validation failed for classes
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='numeric value out of bounds (<9 digits>.<3 digits> expected)...

Upvotes: 1

Views: 1368

Answers (2)

tlavarea
tlavarea

Reputation: 613

So it turns out that the @BasePathAwareController is what's causing the binding validation to not run. Not sure if this is intended behavior or a bug, but by removing that annotation from my controller, the validation runs as expected.

Upvotes: 1

Yuri Plevako
Yuri Plevako

Reputation: 321

Using @Valid annotation you should declare BindingResult parameter right after validated pojo. That BindingResult is gonna contain all validation errors and you are free to do whatever you want with them.

Upvotes: 0

Related Questions