0

Custom annotation, as defined https://dzone.com/articles/bean-validation-and-jsr-303

Can we use @Capitalized annotation in controller?

e.g.

 @RestController

public class Abc {

@RequestMapping(value="/abc", method=RequestMethod.POST)

public String abc(@Capitalized @RequestParam(value="abc") String abc) {



}

}

I used in this way, but it is not working. Any idea why it is not working?

Thanks,

Akkave
  • 303
  • 2
  • 4
  • 17

3 Answers3

0

Add Parameter ElementType in annotation target, then it will work.

E.g.:

@Target(ElementType.PARAMETER)
Sachin Gupta
  • 7,805
  • 4
  • 30
  • 45
  • Sachin, Does it need any config change to work like other JSR validation works e.g. Min, Size etc.? – Akkave Oct 06 '17 at 10:21
0

Assuming that @Capitalized is:

@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = CapitalizedValidator.class)
@Documented
public @interface Capitalized {

    String message() default "should be capital";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

and you have a constraint validation impl as:

public class CapitalizedValidator implements ConstraintValidator<Capitalized, String> {

    private String message;
    @Override
    public void initialize(Capitalized constraintAnnotation) {
        message = constraintAnnotation.message();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        String inUpperCase = value.toUpperCase();

        if (inUpperCase.equals(value)) {
            return true;
        }

        context.buildConstraintViolationWithTemplate(message);

        return false;
    }

}

Then try this with you're controller:

@Validated
@RestController
public class SampleController {

    @RequestMapping(method = RequestMethod.POST)
    public String post(@Capitalized @RequestParam("content") String content) {
        return content;
    }

}
geneqew
  • 2,401
  • 5
  • 33
  • 48
  • Thanks, I have same setup as u mentioned. But its not working – Akkave Oct 06 '17 at 11:38
  • i found some link regarding this [here](https://stackoverflow.com/questions/27526620/dependency-injection-in-jsr-303-constraint-validator-with-spring-fails/27824912#27824912).... Not getting why bean config needed? – Akkave Oct 06 '17 at 11:41
  • Could you include the version of dependencies in your question? ie spring-boot, hibernate-validator version? – geneqew Oct 09 '17 at 02:37
0

After including below code in Application.java, Its working fine.

@Bean
    public Validator validator() {
return new LocalValidatorFactoryBean();
 }

  @Bean
   public MethodValidationPostProcessor methodValidationPostProcessor() {
MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor();
methodValidationPostProcessor.setValidator(validator());
return methodValidationPostProcessor;

}

Akkave
  • 303
  • 2
  • 4
  • 17