0

How can i check if "${path}" value is not empty or is correct path and if so, throw some exception? I want to it happen during Bean creation. All I found that is such validation used in multilayer apps for user input data validation in @Controller classes with using @Valid annotation. But what i did was not working.

It a simple Spring app, that reading application.properties and somehow processes them. Here is my code:

@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;
    }
 }

Can I do this and if so, What am I doing wrong?

I tried this:

@Component
public class FileGuider {
public List<File> search(@Valid String path, String sourceFileExtension)
  throws IOException, NoFilesToParseException {...}

P.S. I use Spring in this app for studying.

  • Here where did u added @valid annotation? – Jay Jul 18 '18 at 19:44
  • I think you can find answer for your question [here](https://stackoverflow.com/questions/31176561/spring-value-adding-validation-less-than) – aarexer Jul 18 '18 at 21:25
  • @jai I updated my post. – Andrei Shalkevich Jul 19 '18 at 06:06
  • What you are trying to achieve, plz say clearly – Jay Jul 19 '18 at 06:18
  • @jai I want to validate injected property while bean creating with custom annotation and throw readable exception for user with message, for example "Is empty or not valid path.". Also i tried to use my annotation after bean created in search() method, but there is no effect. – Andrei Shalkevich Jul 19 '18 at 06:35
  • @jai Are you deleted your post? Thanks for your answer, but i have simple spring app and injecting properties by Value annotation and wand to validate by my custom annotation. Is it possible or i must do that by hand, because i want to check if it correct path? Cheers – Andrei Shalkevich Jul 19 '18 at 06:59
  • I have misunderstood your question. So in that answer i have explained how to use _@value_ and _@valid_ annotations – Jay Jul 19 '18 at 07:35
  • just check my answer if its not helpful or irrelevant i will delete my anwser – Jay Jul 19 '18 at 07:42
  • @jai Thanks again. But I meant a little different. )) – Andrei Shalkevich Jul 19 '18 at 08:27

1 Answers1

0

Here you find an example

Controller

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;
    }

Next LoginReq Request class with validation

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;
    }
}

For more information GitHubLink. if you have still problem feel free and ask here.

Faiz Akram
  • 559
  • 4
  • 10