Issue
I implement my own validation to check value of capacity od Budget entity in my SpringApp. Code below :
Annotation :
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Constraint(validatedBy = {CapacityValidator.class})
public @interface Capacity {
String message() default "Capacity over limit. Maximum value 9999";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
CapacityValidator class :
import com.example.CapacityGurdian.annotation.Capacity;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class CapacityValidator implements ConstraintValidator<Capacity, Float> {
private final static float MAX_CAPACITY_VALUE = 9999;
@Override
public boolean isValid(Float value, ConstraintValidatorContext constraintValidatorContext) {
return MAX_CAPACITY_VALUE < value;
}
}
Then I used it in REST controller like this :
@RestController
@RequestMapping("/budgets")
@Validated
@Api(
value = "GrantsBudgetController",
tags = "Capacity controller for budget"
)
public class BudgetController {
BudgetService budgetService;
@Autowired
public BudgetController(BudgetService budgetService) {
this.budgetService = budgetService;
}
@PutMapping({"/budget/{id}/capacity/{value}"})
@ApiOperation(value = "Updates capacity od specified budget by id",
notes = "Adds value to capacity of specified budget",
response = Budget.class)
public ResponseEntity<Budget> updateBudget(
@ApiParam(value = "Id of updated budget") @PathVariable @NotNull Long id,
@ApiParam(value = "Value which will be added to current capacity") @PathVariable
@Capacity Float value) {
return ResponseEntity.ok(budgetService.addCapacityById(id,value));
}
This code should work but not this time. Then I decided to check if standard javax.validations work by adding @Size( max = 3) and @NotEmpty on String property. Unfortunetly it also doesnt work. I checked Annotation processor in my intellij and it is set fine. Do someone know how to turn on my validations ?
Code of pom.xml :
<?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>
<groupId>com.example</groupId>
<artifactId>CapacityGurdian</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CapacityGurdian</name>
<description>CapacityGuardianApp</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- JPA - DATABASE-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>true</addResources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
</project>
Solution
Your dependencies don't contain hibernate-validator
, try to add it like that:
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
Answered By - Dmitrii Bykov