We are using SpringBoot version 2.7.9, in which I am trying to switch off the automatic validation of the DTOs. However, this does not seem to work correctly in my case. We have defined the whole interface in OpenApi 3 and let it generate the DTOs and the interfaces for Spring. This all works very well and as desired. So far so good.
Now we have defined the following max and min values in the specification:
UserDTO1:
type: object
properties:
ia_subject:
type: string
description: Subject of the user.
example: c2f3fb56-4194-4af6-a007-9fe403a2abeb
maxLength: 36
minLength: 36
email:
type: string
example: john.doe@example.com
maxLength: 255
firstname:
type: string
example: John
maxLength: 255
lastname:
type: string
example: Doe
maxLength: 255
accounts:
type: array
items:
"$ref": "#/components/schemas/AccountDTO1"
This generates the following annotations on the DTO in Java:
...
import javax.validation.constraints.Size;
...
/**
* Subject of the user.
* @return iaSubject
*/
@Size(min = 36, max = 36)
public String getIaSubject() {
return iaSubject;
}
This also works without any problems. The fields in the body / in the DTOs are validated correctly and Spring returns a generic error. Now I don't want this Spring error, but our own implementation. Spring should not care whether the fields are valid according to the specification or not. I want to switch this off and validate the DTOs myself like with the Hibernate Validator:
...
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Object>> validate = validator.validate(dto);
....
The problem now is, I can't disable SpringBoot validation. I found the following questions and properties:
How to disable Hibernate validation in a Spring Boot project
I set:
spring.jpa.properties.javax.persistence.validation.mode=none
in my application.properties but it is still validated and the generic error response from spring remains. (All other settings in application.properties work without problems.)
Actually it is not the JPA validation but a kind of DTO validation.
- Is my approach a good idea?
- Is there a better solution?
- (I don't want to use filters because at this point the body is still a string and has not yet been processed by Jackson).
We found out that we can remove the @Valid annotations in the generated interface. This way, the DTO is no longer validated and Spring no longer returns the generic answer. But we would have to do this with a post-processing script or in bash after generation.
Unfortunately I can't find any good and further information on this.
Can someone point me in the right direction?
Thank you and kind regards.