【问题标题】:Is there a way to validate Strings parameters in spring boot? Issues with commas in strings有没有办法在 Spring Boot 中验证字符串参数?字符串中的逗号问题
【发布时间】:2021-07-23 22:24:07
【问题描述】:

我为 get 请求设置了一个 api 端点。参数是一个字符串数组,其中每个字符串是一个字符分隔的数字列表,如 ["dr:1/2/3"]

@GetMapping("/getmapping")
public ResponseEntity<JsonNode> getRequest(String[] list) {
...
}

问题是当我传入这样的字符串时 ["dr:1,2,3"]

并且参数会自动拆分为 ["dr:1", "2", "3"]

有没有办法阻止字符串自动拆分?

除了逗号之外,我可以使用不同的字符来分隔数字,它会起作用,但是我将如何验证是否使用了逗号以便抛出适当的错误?

【问题讨论】:

  • “博士”的目的是什么?如果您可以控制参数,那么参数类型可能应该是Integer[] 而不是String[]

标签: java spring spring-boot rest


【解决方案1】:

不,你想要的是 spring 支持的一些非常自定义的行为。

你可以做的是以下

@GetMapping("/getmapping")
public ResponseEntity<JsonNode> getRequest(String param) {
   String[] parts = param.split("/"); //Here you have the information as you want it to
...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 2021-11-18
    • 2014-04-07
    • 1970-01-01
    相关资源
    最近更新 更多