英文:
How can I skip `javax.validation` during update/PATCH
问题
我有以下示例实体:
@Entity
@Table(name = "Contact")
@Data
public class Contact {
Long Id;
String phoneNumber;
@javax.validation.constraints.Email(message = "A valid email address is required")
private String emailAddress;
String type;
}
我正在更新现有记录的类型,来自 Contact
表。
现有记录如下:
{
"phoneNumber": "1234567890",
"emailAddress": "testEamail",
"type": "1"
}
我将类型更改为2,使用 PATCH
请求。但是我收到错误消息,指出现有的电子邮件格式无效。我无法更改现有的电子邮件地址。
你有什么办法可以在更新/应用 PATCH
时跳过 javax.validation
?
当我们在服务类中保存现有联系人时,我收到错误。
服务类代码如下:
Contact contact = contactRepository.getById(id);
contact.setType(2);
contactRepository.save(contact);
英文:
I have my entity as shown below
@Entity
@Table(name = "Contact")
@Data
public class Contact{
Long Id;
String phoneNumber;
@javax.validation.constraints.Email(message = "A valid email address is required")
private String emailAddress;
String type;
}
I am updating the type for the existing record from Contact
table
existing record
{
"phoneNumber":"1234567890";
"emailAddress":"testEamail"
"type":"1"
}
I am changing type to 2 with PATCH
request.
But I am getting error "A valid email address is required" as existing email is in valid format.
I can't change existing emailAddress.
Any idea how can I skip javax.validation
during update/PATCH
I am getting an error when we save existing contact in service class
service class code
Contact contact = contactRepository.getById(id);
contact.setType(2);
contactRepository.save(contact);
答案1
得分: 1
验证注释具有“group”参数,允许将它们分配给验证组。
Bean Validation和JPA很好地集成在一起:
> JPA规范定义了一组配置参数,用于配置在执行插入、更新和删除操作之前应验证哪个验证组。
javax.persistence.validation.group.pre-persist
javax.persistence.validation.group.pre-update
javax.persistence.validation.group.pre-remove
参见:
英文:
Validation annotations have a group
parameter which allows to assign them to validation groups.
Bean Validation and JPA integrate nicely:
> The JPA specification defines a set of configuration parameters to configure which ValidationGroups shall be validated before performing an insert, update, and remove operations.
favax.persistence.validation.group.pre-persist
javax.persistence.validation.group.pre-update
javax.persistence.validation.group.pre-remove
See:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论