404年春季控制器错误,如果JSON字符串中包含大于10位数的数字。

huangapple go评论76阅读模式
英文:

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

问题

@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 a 404 error. The request won't even reach the controller. However, if I reduce the number of digits to less than 10, say 123456789, I get the expected response. The reason I'm keeping the request body as a String is that sometimes the request may be a GraphQL String or a JSON String, but this occurs for both scenarios.

英文:

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

得分: 2

这可能是因为在Java中,userId字段的类型是Integer,而Integer类型的范围可能导致的。

在Java中,int的范围是-2,147,483,648 到 2,147,483,647。

因此,请尝试在这个范围内和范围外进行测试,检查是否在范围内正常工作,在范围外不正常工作,如果是这样,那就是问题所在。

在这种情况下,您将不得不将int更改为long数据类型。

英文:

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

得分: 1

尝试将其更改为 Long 类型。

int 的最大值为 2147483647,而 Long 的最大值为 9223372036854775807。

英文:

Try changing it to Long type instead.

Max value for int is 2147483647 while Long is 9223372036854775807

答案3

得分: 0

可能的原因是usedId使用的数据类型。

对于int,范围是2,147,483,647,尝试将数据类型更改为具有范围直至9,223,372,036,854,775,807long

英文:

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

huangapple
  • 本文由 发表于 2020年10月16日 12:11:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64382824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定