0

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

@PostMapping("/addUser")
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 Answers3

2

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.

Alien
  • 15,141
  • 6
  • 37
  • 57
  • I'm getting the entire JSON itself as a String, any idea how i can turn it into Long? – vishal sundararajan Oct 16 '20 at 05:38
  • I would suggest you to create a DTO so that you don't have to do manual json parsing...that would be more convenient and efficient way. – Alien Oct 16 '20 at 05:45
  • The problem is the controller serves as a proxy which just forwards the incoming request by converting the input to JSON , so i don't know what kind of property i will get in request body – vishal sundararajan Oct 16 '20 at 05:47
1

Try changing it to Long type instead.

Max value for int is 2147483647 while Long is 9223372036854775807

  • I tried changing it to Long type, the request did not even reach the controller and directly thrown 404 spring error, is there any tomcat or servlet config that i'm not aware of? – vishal sundararajan Nov 09 '20 at 16:22
0

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

Mario Codes
  • 689
  • 8
  • 15
Deepika
  • 41
  • 3
  • I'm getting the entire JSON itself as a String, any idea how i can turn it into Long because userId is a property of that JSON ? The problem is the spring does not even let me get the input since it does not even let the request inside the controller – vishal sundararajan Oct 16 '20 at 05:39