我如何在更新/PATCH 期间跳过 `javax.validation`?

huangapple go评论79阅读模式
英文:

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 PATCHrequest.
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:

huangapple
  • 本文由 发表于 2020年5月5日 16:23:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61608786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定