Spring NumberFormatException:无法将“java.lang.String”类型的值转换为所需类型“java.lang.Long”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32679451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring NumberFormatException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long
提问by Simon
I'm not sure if I'm missing something really basic but this is what I would like to do.
我不确定我是否遗漏了一些非常基本的东西,但这就是我想做的。
I would like to make a rest API call to this address:
我想对这个地址进行一个休息 API 调用:
https://localhost:8080/fetchlocation?lat=-26.2041028&lng=28.0473051&radius=500
https://localhost:8080/fetchlocation?lat=-26.2041028&lng=28.0473051&radius=500
My rest method is:
我的休息方法是:
public void fetchlocation(@RequestParam Long lat, @RequestParam Long lng, @RequestParam int radius){ //fetches location}
I get this error:
我收到此错误:
"timestamp": 1442751996949, "status": 400, "error": "Bad Request", "exception": "org.springframework.beans.TypeMismatchException",
"message": "Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: \"-26.2041028\"",
"path": "/fetchlocation"
“时间戳”:1442751996949,“状态”:400,“错误”:“错误请求”,“异常”:“org.springframework.beans.TypeMismatchException”,
“消息”:“无法转换类型'java.lang的值.String' 到所需类型 'java.lang.Long';嵌套异常是 java.lang.NumberFormatException:对于输入字符串:\"-26.2041028\"",
"path":"/fetchlocation"
I guess it is because rest API receives the co-ordinates as Strings instead of Longs when I make the GET call. How do I ensure that the rest API gets the Longs and not the String value when the call is made?
我猜这是因为当我进行 GET 调用时,rest API 将坐标作为字符串而不是 Longs 接收。我如何确保在调用时其余 API 获得 Longs 而不是 String 值?
I could easily get the method to work when I change the rest API method to take Strings as the parameters, but for type checking, I would prefer to use Longs.
当我更改 rest API 方法以将字符串作为参数时,我可以轻松地使该方法工作,但是对于类型检查,我更喜欢使用 Longs。
public void fetchlocation(@RequestParam String lat, @RequestParam String lng, @RequestParam String radius){ //fetches location}
采纳答案by Aditya Singh
The number you are trying to convert to java.lang.Long
is -26.2041028
.
您尝试转换为的数字java.lang.Long
是-26.2041028
。
Your number contains a .
(decimal). This is not allowed for Long
s. You need to use java.lang.Double
.
您的号码包含一个.
(十进制)。这对于Long
s是不允许的。您需要使用java.lang.Double
.
And also succeed your number with an L
for Long
or a D
for Double
for static initializations. Even though not required, it makes the code more readable.
并且还带有一个成功的数量L
为Long
或D
用于Double
静态初始化。尽管不是必需的,但它使代码更具可读性。
Like, -26.2041028D
for Double
.
就像,-26.2041028D
对于Double
。
回答by zulkarnain shah
You need to make sure that the String that you're converting to Long contains onlynumbers. That means for example a string like this, "10d"isn't convertible to a Long, hence the exception.
您需要确保要转换为 Long 的 String仅包含数字。这意味着例如像这样的字符串, “10d”不能转换为 Long,因此是例外。