简体   繁体   中英

Getting NumberFormatException for Spring Boot rest API

I am working on the following API in Spring Boot:

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

I am implementing the rest API as follows:

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

While calling the API as: /fetchStudents?prefix='a'&fetchSize=50 , I am getting NumberFormatException . I am not getting how to fix this issue.

The issue might come from the a param that is surrounded by ` signs.

Since you send it in the URL it should be encoded to %60a%60

So it seems like Spring is not reading the first param correctly and that effects the second param of fetchSize

Try to eliminate parts of the query params to better identify the problem:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM