I know there are already several questions on this.
I started a personal project on Spring Boot version 2.7.9 and wanted to use field level validation using @NotNull, @NotBlank, etc. After coding accordingly, it turned out the code is not able to detect the error. I came across this question and found that starter-validation is required to be explicitly mentioned. I added and restarted, but still it did not work. I scrolled down the same question and found one answer that IDE needs to be restarted as well. After doing so, it started working.
A couple of days later, I started working on it again. I added some business logic and some config like request logging. However, this time the annotation stopped working again.
I tried:
- Removing starter-validation dependency and re-added, restarted IDE
- Removing whole requestlogging component (using OncePerRequestFilter) just for peace of mind.
- Checked a few more questions here. All talk about missing dependency only. From here, I added the two properties as well.
Nothing has worked so far. Did someone also face a similar issue?
Observation: I don't know if it helps or not, although code fails to detect the error, I do get ConstraintViolationException when it tries to persist the entity.
My Pom.xml: (dependencies section only)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.15</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>
Controller:
@PostMapping(value = "/create")
public ResponseEntity<MyDetailedResponse> createClient(
@Valid @RequestBody MyRequest request) {
//business logic
}
EDIT 1: Entity class as requested:
@Entity
@Audited
@Table(name = "client_details")
@JsonIgnoreProperties(value = { "optlock", "createdDate", "createdBy", "lastModifiedDate",
? ? ? ? "lastModifiedBy" }, ignoreUnknown = true)
public class AppClientDetails extends EntityVersionedMetaData {
? ? /**
? ? ?*?
? ? ?*/
? ? private static final long serialVersionUID = 756964595629774899L;
? ? @NotBlank(message = "Name can not be blank")
? ? private String fullName;
? ? @NotBlank(message = "Please choose a short code for the client")
? ? private String shortCode;
? ? @NotBlank(message = "Address is mandatory.")
? ? @Size(min = 1, max = 255)
?// @Pattern(regexp = "^([a-zA-Z0-9.&:-\\S+]{1,255})$", message = "Invalid address. Only alphabets, number and . & : - are allowed")
? ? private String address;
? ? @NotBlank(message = "Contact person name is mandatory")
? ? private String contactPerson;
? ? private long phoneNumber;
? ? private String emailId;
? ? private String logoUrl;
? ? @NotNull(message = "Strength is required")
? ? private Short strength;
? ? @Temporal(TemporalType.DATE)
? ? private Date establishedDate;
? ? @Temporal(TemporalType.DATE)
? ? private Date onboardingDate;
? ? @NotBlank(message = "Plan must be selected")
? ? private String currentPlan;
? ? @Enumerated(EnumType.ORDINAL)
? ? private AppUserStatus clientStatus = AppUserStatus.ACTIVE;
//getters setters
}
Just to add, I had commented @Pattern as it was throwing illegal character exception due to the \s in regex. That is also one issue.
EDIT 2: As last resort, I deleted whole .m2 directory and workspace both. Freshly created the whole project. Still it did not work.
@Validated
to the@RestController
class.