404 spring controller error if JSON string contains number greater than 10 digits

I'm having a controller which gets json as string in request body as below

public ResponseEntity<?> addUser (@RequestBody String userJson,HttpServletRequest request) {
   log.info("inside add user controller")
   String responseStatus = serviceInterface.addUser (userJson);
   return new ResponseEntity (responseStatus);

The request body is

 "user": {
   "username": "testuser",
   "userId": 12345678901233,
   "phonenumber": "9876756475",
   "emailaddress": "test@org.com"

The problem i'm facing is when the userId property has more than 10 digits the controller returns 404 error the request won't even reach the controller , but if i reduce the the number of digits to less than 10 say 123456789, i'm getting the actual expected response . The reason i'm keeping the request body as String because sometimes the request maybe a graphql String or a JSON String but this occurs for both scenarios.

3 answers

  • answered 2020-10-16 04:31 Alien

    It might be because of the range for an Integer in java which is the type of userId field.

    The range of an int in java is -2,147,483,648 .. 2,147,483,647.

    So try inside and outside this range and check if it works for the range and doesn't work for out of range then that's the issue.

    In that case you will have to change int to long data type.

  • answered 2020-10-16 04:34 Irshaad Moosuddee

    Try changing it to Long type instead.

    Max value for int is 2147483647 while Long is 9223372036854775807

  • answered 2020-10-16 05:38 Deepika

    The possible reason behind this is the data type used for usedId.

    For int, the range is until 2,147,483,647 try changing the data type to long which has range until 9,223,372,036,854,775,807