java - 获取 Spring Boot REST API 的 NumberFormatException

标签 java spring spring-boot

我正在 Spring Boot 中开发以下 API:

/fetchStudents?prefix=<prefix>&fetchSize=<fetchSize>

我正在实现其余 API,如下所示:

  @GetMapping("/fetchStudents")
   public ResponseEntity<List<Student> getStudents(@RequestParam String prefix,
                                                   @RequestParam(defaultValue="50") int fetchSize) {
   ....
  }

在调用 API 时:/fetchStudents?prefix='a'&fetchSize=50,我收到 NumberFormatException。我不知道如何解决这个问题。

最佳答案

问题可能来自于 ` 符号包围的 a 参数。

由于您在 URL 中发送它,因此应将其编码为 %60a%60

看来 Spring 没有正确读取第一个参数,这会影响 fetchSize 的第二个参数

尝试消除部分查询参数以更好地识别问题:

  • /fetchStudents?fetchSize=50
  • /fetchStudents?fetchSize=50&prefix='a'
  • /fetchStudents?fetchSize=50&prefix=%60a%60

关于java - 获取 Spring Boot REST API 的 NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62145344/

相关文章:

java - 记录对象何时被垃圾回收

java - Log4j2 rc 1 DefaultRolloverStrategy 在 7 个文件后覆盖

java - Spring MVC 中的部署失败,并且未找到带有 URI 的 HTTP 请求的映射

java - 运行 npm build 时无法访问 vue.js 前端(frontend-maven-plugin 和 spring-boot 后端)

java - 如何通过API获取windchill中的序号

java - 将 JTable 中的数据导出为 PDF 格式

java - Spring Boot 忽略来自外部 application.properties 的日志记录级别

java - 我可以在 Spring Cloud Config Server 中托管 JSON 文件吗?

java - Flyway自动生成迁移SQL

java - Spring Boot - 自定义异常处理程序