【问题标题】:validation of the entered field输入字段的验证
【发布时间】:2019-07-29 09:55:35
【问题描述】:

在我的应用程序中,我将fromCurrencytoCurrencyamount 这三个参数的值输入到地址栏中 并在控制器中。我想检查输入数据的正确性。但是我在测试期间产生了一个异常,没有进一步 那些。我需要一个代码,控制器中的代码将检查输入数据的正确性,并且根据发生错误的字段,将产生第 400 个错误,其名称为错误填写的字段

我试过这个验证,用

if(!Currency.getAvailableCurrencies().contains(Currency.getInstance(fromCurrency)))

但如果 Currency 不包含 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);
    }
}

【问题讨论】:

  • 将您的 fromCurrencytoCurrencyString 更改为 Currencyamount 更改为 BigDecimal。如果 Spring 不能转换,Spring 将进行转换或异常。如果您真的想坚持使用String(我建议不要这样做),请使用表单对象并编写Validator 来进行验证。

标签: java spring-boot exception currency


【解决方案1】:

您可以使用 Hibernate Validator 来验证控制器的 @RequestParam

将此依赖项添加到您的pom.xml

<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.10.Final</version>
</dependency>

然后您必须通过像这样添加@Validated 注释来启用控制器中的请求参数和路径变量的验证

@RestController
@RequestMapping("/")
@Validated
public class Controller {
    // ...
}

然后您可以将 @NotNull @Min @Max 之类的注释添加到您的 RequestParam Like

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

您还可以根据需要定义自定义注释。

还有更详细好看的文章here

【讨论】:

    猜你喜欢
    • 2021-12-11
    • 1970-01-01
    • 2023-03-14
    • 2011-04-25
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    相关资源
    最近更新 更多