2

I'm trying to use immutable library https://immutables.github.io/ and validate the fields using javax.validation but the annotations are just ignored when the class is generated. Even on unit testing it just doesn't throw any error.

    import javax.validation.Valid;
    import javax.validation.constraints.Size;
    import org.immutables.value.Value;

    @Value.Immutable
    public interface Entity {
        @Valid @Size(max = 10) 
        String name();
    }

my pom.xml, I tried to add spring-boot-starter-validation and also javax.validation but it just doesn't trigger the validations

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.4</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>immutableTest</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>immutableTest</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-validation</artifactId>
            </dependency>
            <dependency>
                <groupId>org.immutables</groupId>
                <artifactId>value</artifactId>
                <version>2.8.8</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.assertj</groupId>
                <artifactId>assertj-core</artifactId>
                <version>3.21.0</version>
                <scope>test</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
            <dependency>
                <groupId>javax.validation</groupId>
                <artifactId>validation-api</artifactId>
                <version>2.0.1.Final</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.12.5</version>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
John
  • 1,697
  • 4
  • 27
  • 53

1 Answers1

-1

You need to enable annotation processor in your IDE.
If you are using intellij, Settings > Compiler > Annotation Processors.

Sam Jing Wen
  • 63
  • 1
  • 6
  • Thank you for your help. Now with a regular class I already have the validation, But not with the immutables, it seems that the validations are not being carried to the generated class. – John Sep 27 '21 at 20:06
  • The validation annotations won’t generate additional code. To use them would be something like this: ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); validator.validate(entity) – Sam Jing Wen Sep 27 '21 at 23:46
  • But I cannot use it like this as Controller parameter like this "@RequestBody @Valid ImmutableEntity entity" ? – John Sep 28 '21 at 10:46
  • It should work after adding @Validated to your controller class – Sam Jing Wen Sep 28 '21 at 13:47
  • 1
    It didn't work, but I think that I've found the problem. It works if I rename the parameters of my immutable with prefix "get" , like " getName(), getAge(). But this is a bit odd to need to add this prefix. – John Sep 28 '21 at 17:08
  • Usually for boilerplate code, lombok is the more popular one – Sam Jing Wen Sep 29 '21 at 03:21