我有我的 RequestParam,我需要验证它,但是 mu 自定义验证不适用,我的控制器
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
和自定义验证
public class ConstractCurrencyValidator implements
ConstraintValidator<CurrencyValidation, String> {
@Override
public void initialize(CurrencyValidation currency) {
} …Run Code Online (Sandbox Code Playgroud) java validation controller http-request-parameters spring-boot