Java 输入字段的验证
在我的应用程序中,我在地址栏中输入三个参数的值,Java 输入字段的验证,java,spring-boot,exception,currency,Java,Spring Boot,Exception,Currency,在我的应用程序中,我在地址栏中输入三个参数的值,从货币,到货币,以及金额 在控制器中。我想检查输入数据的正确性。但是我在测试过程中生成了一个异常,没有进一步的进展 那些我需要一个代码,控制器将检查输入数据的正确性,并根据错误发生的字段,生成第400个错误,该错误的名称为错误填写的字段 我已经试过这个验证了 if(!Currency.getAvailableCurrencies().contains(Currency.getInstance(fromCurrency))) 但如果货币不包含fro
从货币
,到货币
,以及金额
在控制器中。我想检查输入数据的正确性。但是我在测试过程中生成了一个异常,没有进一步的进展
那些我需要一个代码,控制器将检查输入数据的正确性,并根据错误发生的字段,生成第400个错误,该错误的名称为错误填写的字段
我已经试过这个验证了
if(!Currency.getAvailableCurrencies().contains(Currency.getInstance(fromCurrency)))
但如果货币不包含fromCurrency,则生成异常
@RestController
class ExchangeController {
private static final Logger logger = Logger.getLogger(ExchangeController.class.getName());
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
@Autowired
@Qualifier("dataService")
private CurrencyExchangeService currencyExchangeService;
@SuppressWarnings("SameReturnValue")
@RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json")
public String start() {
return "input parameters";
}
@RequestMapping(value = "/convert", method = RequestMethod.GET, produces = "application/json")
public ExchangeRateDTO converting(@RequestParam("fromCurrency") String fromCurrency,
@RequestParam("toCurrency") String toCurrency,
@RequestParam("amount") String amount) throws IOException {
if (!Currency.getAvailableCurrencies().contains(Currency.getInstance(fromCurrency))) {
}
BigDecimal convertedAmount = currencyExchangeService.convert(fromCurrency, toCurrency, new BigDecimal(amount));
return new ExchangeRateDTO(fromCurrency, toCurrency, new BigDecimal(amount), convertedAmount);
}
}
您可以使用Hibernate验证程序验证控制器的
@RequestParam
将此依赖项添加到您的pom.xml
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.10.Final</version>
</dependency>
然后,您可以将诸如@NotNull
@Min
@Max
之类的注释添加到您的请求参数中
@RequestMapping(value = "/convert", method = RequestMethod.GET, produces = "application/json")
public ExchangeRateDTO converting(@RequestParam("fromCurrency") @NotNull @NotBlank @Size(max = 10) String fromCurrency,
@RequestParam("toCurrency") String toCurrency,
@RequestParam("amount") String amount) throws IOException {
if (!Currency.getAvailableCurrencies().contains(Currency.getInstance(fromCurrency))) {
}
BigDecimal convertedAmount = currencyExchangeService.convert(fromCurrency, toCurrency, new BigDecimal(amount));
您还可以根据需要定义自定义注释
还有一篇更详细、更精彩的文章将您的
从货币
和更改为货币
从字符串
更改为货币
和金额
更改为大十进制
。Spring将执行转换,如果无法转换,则执行异常。如果你真的想坚持使用String
(我建议你不要这样做),使用一个表单对象并编写一个Validator
来进行验证。
@RequestMapping(value = "/convert", method = RequestMethod.GET, produces = "application/json")
public ExchangeRateDTO converting(@RequestParam("fromCurrency") @NotNull @NotBlank @Size(max = 10) String fromCurrency,
@RequestParam("toCurrency") String toCurrency,
@RequestParam("amount") String amount) throws IOException {
if (!Currency.getAvailableCurrencies().contains(Currency.getInstance(fromCurrency))) {
}
BigDecimal convertedAmount = currencyExchangeService.convert(fromCurrency, toCurrency, new BigDecimal(amount));