Java 验证请求正文不等于模式
在验证请求参数时,我正在进行非空检查,但现在我想阻止某些类型的模式Java 验证请求正文不等于模式,java,spring,micronaut,Java,Spring,Micronaut,在验证请求参数时,我正在进行非空检查,但现在我想阻止某些类型的模式 @Prototype public class MyObj { @NotBlank(message="Error: id cannot be null") @JsonProperty("id") private String id; } ^(?!(\*\s+(\w+)\s+\*)$)(?!((\w+)\:(\d+)\/(\d+))$).* 现在我必须阻止id与以
@Prototype
public class MyObj {
@NotBlank(message="Error: id cannot be null")
@JsonProperty("id")
private String id;
}
^(?!(\*\s+(\w+)\s+\*)$)(?!((\w+)\:(\d+)\/(\d+))$).*
现在我必须阻止id与以下任何模式匹配的请求
(\*\s+(\w+)\s+\*)
((\w+):(\d+)/(\d+)
我知道我可以包含一个@模式(regexp=
,以允许特定模式,但不确定如何阻止特定模式。还可以执行多个模式验证。您可以在@Pattern注释中使用它来排除两个指定模式
@Prototype
public class MyObj {
@NotBlank(message="Error: id cannot be null")
@JsonProperty("id")
private String id;
}
^(?!(\*\s+(\w+)\s+\*)$)(?!((\w+)\:(\d+)\/(\d+))$).*
您可以在@Pattern注释中使用以排除两个指定的模式
@Prototype
public class MyObj {
@NotBlank(message="Error: id cannot be null")
@JsonProperty("id")
private String id;
}
^(?!(\*\s+(\w+)\s+\*)$)(?!((\w+)\:(\d+)\/(\d+))$).*
如果您仍然计划通过注释来实现,您可以创建自己的注释。 此处使用micronaut作为样本:
@原型
公共类MyObj{
@NotAllowedPattern(值={onePattern,secondPattern})
@JsonProperty(“id”)
私有字符串id;
}
@独生子女
ConstraintValidator notAllowedPattern(){
返回(值、注释元数据、上下文)->{
//根据注释,模式应该是强制性的
可选annotationPatterns=annotationMetadata.stringValue(“值”);
String[]patterns=annotationPatterns.get();
//如果来自not allowed的单个模式匹配,则应返回false
//并引发导致错误消息的ConstraintViolationException
。
返回DoesNotCharlPatterns(值、模式);
};
}
如果您仍然计划通过注释来完成,您可以创建自己的注释。
此处使用micronaut作为样本:
@原型
公共类MyObj{
@NotAllowedPattern(值={onePattern,secondPattern})
@JsonProperty(“id”)
私有字符串id;
}
@独生子女
ConstraintValidator notAllowedPattern(){
返回(值、注释元数据、上下文)->{
//根据注释,模式应该是强制性的
可选annotationPatterns=annotationMetadata.stringValue(“值”);
String[]patterns=annotationPatterns.get();
//如果来自not allowed的单个模式匹配,则应返回false
//并引发导致错误消息的ConstraintViolationException
。
返回DoesNotCharlPatterns(值、模式);
};
}