Java 如何使用自定义注释验证带Spring@Value字段的注释?
如何检查“${path}”值是否为空或路径是否正确,如果是,则引发一些异常?我希望它在Bean创建期间发生。 我所发现的就是在多层结构中使用这种验证 使用@Valid annotation在@Controller类中验证用户输入数据的应用程序。 但我所做的并没有起作用 它是一个简单的Spring应用程序,读取application.properties并以某种方式处理它们。 这是我的密码:Java 如何使用自定义注释验证带Spring@Value字段的注释?,java,spring,bean-validation,spring-annotations,Java,Spring,Bean Validation,Spring Annotations,如何检查“${path}”值是否为空或路径是否正确,如果是,则引发一些异常?我希望它在Bean创建期间发生。 我所发现的就是在多层结构中使用这种验证 使用@Valid annotation在@Controller类中验证用户输入数据的应用程序。 但我所做的并没有起作用 它是一个简单的Spring应用程序,读取application.properties并以某种方式处理它们。 这是我的密码: @Component public class ParseService { @Value("${pat
@Component
public class ParseService {
@Value("${path}")
@PathValue
private String path;
}
@Documented
@Constraint(validatedBy = PathValidatorImpl.class)
@Retention(RUNTIME)
@Target({ElementType.FIELD})
public @interface PathValue {
String message() default "Is empty or not valid path!";
}
public class PathValidatorImpl implements ConstraintValidator<PathValue,
String> {
@Override
public void initialize(PathValue pathValue) {
}
@Override
public boolean isValid(String path, ConstraintValidatorContext ctx) {
if (path.isEmpty() || !(new File(path).exists())) {
return false;
} else
return true;
}
}
@组件
公共类解析服务{
@值(“${path}”)
@路径值
私有字符串路径;
}
@记录
@约束(validatedBy=PathValidatorImpl.class)
@保留(运行时)
@目标({ElementType.FIELD})
公共@接口路径值{
字符串消息()默认值“为空或路径无效!”;
}
公共类PathValidatorImpl实现ConstraintValidator{
@凌驾
公共无效初始化(路径值路径值){
}
@凌驾
公共布尔值有效(字符串路径、ConstraintValidatorContext ctx){
if(path.isEmpty()| |!(新文件(path.exists())){
返回false;
}否则
返回true;
}
}
我可以这样做吗?如果可以,我做错了什么
我试过这个:
@Component
public class FileGuider {
public List<File> search(@Valid String path, String sourceFileExtension)
throws IOException, NoFilesToParseException {...}
@组件
公共类文件管理器{
公共列表搜索(@有效字符串路径,字符串源文件扩展名)
抛出IOException,NoFilesToParseException{…}
另外,我在这个应用程序中使用Spring进行学习。这里有一个例子
控制器
import javax.validation.Valid; //Import this Class for valid annotation
@RequestMapping(value = CommonConstants.TOKEN_CREATION , method = RequestMethod.POST)
public ResponseJson tokenCreation(@Valid @RequestBody LoginReq loginReq) {
response.setResponse(authenticationService.authenticateUser(loginReq));
return response;
}
下一个带有验证的LoginReq请求类
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
public class LoginReq {
@NotEmpty
@Email(message ="{NotEmpty.user.email}")
private String userEmail;
@NotEmpty
@Size(min=8, message="{NotEmpty.user.password}")
private String userCredential;
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public String getUserCredential() {
return userCredential;
}
public void setUserCredential(String userCredential) {
this.userCredential = userCredential;
}
}
更多信息。如果您仍然有问题,请随意提问。这里您在哪里添加了@valid annotation?我想您可以找到您问题的答案@jai更新了我的帖子。请告诉我您想要实现的目标clearly@jai我想在使用自定义注释创建bean时验证注入的属性,并引发可读的异常对于带有消息的用户,例如“为空或路径无效”。我还尝试在search()方法中创建bean之后使用注释,但没有效果。