1

I'm using Spring Boot 1.5.2.RELEASE version which includes 'hibernate-validator' 5.3.4.Final version.

I'm facing an issue which my implementation class for ConstraintValidator executed twice.

I have attached here source code.

  1. Interface

@RestController @Validated @FunctionalInterface public interface IBinRegistryApi {

/**
 *
 * @param lookupRequest
 * @param nonce
 * @return
 */
@PostMapping(value = "/xxx/yyy/zzz/XYZ", headers="Accept=application/json",
        produces= {MediaType.APPLICATION_JSON_VALUE},
        consumes= {MediaType.APPLICATION_JSON_VALUE})
ResponseEntity getWalletByCard(NonceValid @RequestHeader(value = "X-Captcha-Nonce") String nonce); }
  1. Implementation

@Component @Validated @Slf4j public class BinRegistryApiImpl implements IBinRegistryApi { @Override public ResponseEntity getWalletByCard(NonceValid @RequestHeader(value = "X-Captcha-Nonce") String nonce) { }

  1. NonceValid interface

@Target({ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Documented @Constraint(validatedBy=NonceValidator.class) public @interface NonceValid { String message() default "Nonce is invalid";

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

Class<? extends Payload>[] payload() default {}; }
  1. NonceValidator class

public class NonceValidator implements ConstraintValidator {

@Override
public void initialize(NonceValid constraintAnnotation) {
    // no initialization required
}

@Override
public boolean isValid(String nonce, ConstraintValidatorContext context) {
      **// twice here**
} }

Anyone experience the same issue?

Note 1: I do see the my Custom Validator is belong to 2 groups which is IBinRegistryAPI and Default. That is why it is executed twice I believe

Note 2: If I change the implementation to Validator and Custom Group, the issue is not happening.

Thanks,

Nghia Do
  • 2,588
  • 2
  • 17
  • 31

0 Answers0