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

送分小仙女□ 提交于 2021-02-11 12:36:44

问题


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.


回答1:


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.




回答2:


Try changing it to Long type instead.

Max value for int is 2147483647 while Long is 9223372036854775807




回答3:


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



来源:https://stackoverflow.com/questions/64382824/404-spring-controller-error-if-json-string-contains-number-greater-than-10-digit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!