How to add JSR 303 validation to arguments of a method
<p>I would like to use Hibernate Validator to validate arguments of my method. </p>

<p>I have tried to look-up examples and went through the following example
<a href="https://stackoverflow.com/questions/14829487/jsr-303-validate-method-parameter-and-throw-exception">JSR 303. Validate method parameter and throw exception</a></p>

<p>But the answer does not exactly solve my problem and also the links are expired in the answer. </p>

<p>Here is a sample code of what I have tried. </p>

<pre><code>//removing imports to reduce the length of code. 

/**
 * Created by Nick on 15/06/2016.
 */

@Component
public class ValidationService {

    @Value("${namesAllowed}")
    private String validNames;

    @Autowired
    private Validator validator;

    public boolean validateName(
            @NotEmpty
            @Pattern(regexp = ".*'.*'.*")
            @Email
            @Length(max = 10)
                    String name
    ) {

        Set&lt;ConstraintViolation&lt;String&gt;&gt; violations = validator.validate(name);

        for (ConstraintViolation&lt;String&gt; violation : violations) {
            String propertyPath = violation.getPropertyPath().toString();
            String message = violation.getMessage();
            System.out.println("invalid value for: '" + propertyPath + "': " + message);
        }
        List&lt;String&gt; namesAllowed= Arrays.asList(validNames.split(","));
        if (namesAllowed.contains(name.substring(name.indexOf(".") + 1))) {
            return true;
        }
        return false;
    }
}
</code></pre>

<p>}</p>