Spring Boot REST·@Constraint是否要删除? [英] Spring Boot REST · @Constraint for delete?

查看:101
本文介绍了Spring Boot REST·@Constraint是否要删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot,REST,HATEOAS,Hibernate和PostgreSQL的系统后端上工作.为了进行验证,我开始使用extend org.springframework.validation.Validator的类.它运作良好,但仅适用于前端发出的呼叫.对于在后端进行的呼叫(例如使用EntityManager),它们不会触发.我已经设法通过在ElementType.TYPE中使用@Constraint来在这种情况下调用另一个验证器,但是只能在createsave方法中调用它.

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是否要删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆