1
votes

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)...
2
Change @RequestBody to @ModelAttribute if you are using Spring form in front end...show the front end code(how u calling this request) if possible - Prasanna Kumar H A
It's a REST endpoint, so I'm just doing a POST with a JSON object in the body using the Postman Client. - tlavarea
this and this link may help you - Prasanna Kumar H A
I found both of those links previously and tried those solutions. I've also tried different versions of hibernate validator and the validation api libraries to no avail. Spring refuses to validate the request body. - tlavarea

2 Answers

1
votes

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.

0
votes

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.