2

I have my RequestParam and I need to validate it, but mu custom validation don't apply, my controller

@RestController
@Validated
class ExchangeController {

    private static final Logger logger = Logger.getLogger(ExchangeController.class.getName());

    @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
    @Autowired
    @Qualifier("dataService")
    private CurrencyExchangeService currencyExchangeService;

    @RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json")
    public Object converting(@RequestParam("fromCurrency") @NotNull @CurrencyValidation String fromCurrency,
                             @RequestParam("toCurrency") @NotNull String toCurrency,
                             @RequestParam("amount") @NotNull String amount) throws IOException {
        BigDecimal convertedAmount = currencyExchangeService.convert(fromCurrency, toCurrency, new BigDecimal(amount));
        return new ExchangeRateDTO(fromCurrency, toCurrency, new BigDecimal(amount), convertedAmount);

    }
}

and custom validation

public class ConstractCurrencyValidator implements
            ConstraintValidator<CurrencyValidation, String> {
        @Override
        public void initialize(CurrencyValidation currency) {
        }

        @Override
        public boolean isValid(String currency, ConstraintValidatorContext constraintValidatorContext) {
            return currency != null && Currency.getAvailableCurrencies().contains(Currency.getInstance(currency));
        }
    }
9
  • Post your CurrencyValidation class and what is your expected outcome here? Commented Jul 29, 2019 at 13:48
  • @rimonmostafiz I'm expect that if I input not currency I will receive 400 error Commented Jul 29, 2019 at 13:57
  • What error are you receiving? Commented Jul 29, 2019 at 14:01
  • @rimonmostafiz not applicable to parameter Commented Jul 29, 2019 at 14:03
  • Is there any ConstraintViolationException in the log? Commented Jul 29, 2019 at 14:07

2 Answers 2

5

need to put an annotation in my @interface CustomValidation. This means that validation can also be used on the parameter.

@Target({ ElementType.PARAMETER })
Sign up to request clarification or add additional context in comments.

1 Comment

Include all this in your question, so that it might help someone in the future
1

Enable parameter validation in config:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.