问题描述
我正在使用Spring Boot,REST,HATEOAS,Hibernate和PostgreSQL的系统后端上工作.为了进行验证,我开始使用extend org.springframework.validation.Validator
的类.它运作良好,但仅适用于前端发出的呼叫.对于在后端进行的呼叫(例如使用EntityManager
),它们不会触发.我已经设法通过在ElementType.TYPE
中使用@Constraint
来在这种情况下调用另一个验证器,但是只能在create
和save
方法中调用它.
I'm working on a system's back end that uses Spring Boot, REST, HATEOAS, Hibernate and PostgreSQL. For validation, I started using classes that extend org.springframework.validation.Validator
. It works well, but only for calls made by the front end. For calls made in the back end, such as by using EntityManager
, they don't fire. I've managed to have another validator being called in this situation by using @Constraint
for ElementType.TYPE
, but it only gets called for create
and save
methods.
是否也可以使用此验证器对delete
方法进行验证?有一个项目此处我正在研究的项目的运营子集,其中包含我提到的验证器.
Is it possible to use this validator to validate on delete
methods too? There's a project here that's a non operational subset of the project I'm working on, containing the validators I mentioned.
谢谢.
PS:我宁愿避免在后端调用存储库方法时手动调用验证器.
P.S.: I'd rather avoid manually calling the validators whenever I call a repository method in the back end.
PPS: 此答案使我相信这是可能的,但我无法翻译XML配置为JavaConfig.
P.P.S.: This answer makes me believe it's possible, but I couldn't translate the XML configuration to JavaConfig.
推荐答案
我终于找到了答案.在application.properties
中,添加:
I finally found the answer. In application.properties
, add:
spring.jpa.properties.javax.persistence.validation.group.pre-remove=javax.validation.groups.Default
链接的问题告诉我我需要哪个财产,但我不知道将其放在何处.我尝试使用自定义Java配置,甚至使用persistence.xml
配置,但其他几项失败.
The linked question told me which property I needed, but I didn't know where to place it. I tried to use custom Java configuration and even persistence.xml
configuration, but several other things failed.
此处 ,我了解到"[...]创建本地EntityManagerFactory
时,spring.jpa.properties.*
中的所有属性都作为普通的JPA属性(前缀被去除)传递."所以我只添加了该前缀即可使用.
Here, I learned that "[...] all properties in spring.jpa.properties.*
are passed through as normal JPA properties (with the prefix stripped) when the local EntityManagerFactory
is created." So I just added that prefix and it worked.
这篇关于Spring Boot REST·@Constraint是否要删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!