gpt4 book ai didi

java - 无法将自定义验证应用于 requestParam

转载 作者:行者123 更新时间:2023-11-30 10:01:52 26 4
gpt4 key购买 nike

我有我的 RequestParam,我需要验证它,但是 mu 自定义验证不适用,我的 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);

}
}

和自定义验证

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

最佳答案

需要在我的@interface CustomValidation 中添加注释。这意味着验证也可以用于参数。

@Target({ ElementType.PARAMETER })

关于java - 无法将自定义验证应用于 requestParam,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57255179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com