【发布时间】:2015-09-20 12:32:48
【问题描述】:
我不确定我是否遗漏了一些非常基本的东西,但这是我想做的。
我想对这个地址进行一个 REST API 调用:
https://localhost:8080/fetchlocation?lat=-26.2041028&lng=28.0473051&radius=500
我的休息方法是:
public void fetchlocation(@RequestParam Long lat, @RequestParam Long lng, @RequestParam int radius){ //fetches location}
我收到此错误:
“时间戳”:1442751996949,“状态”:400,“错误”:“错误请求”, “异常”:“org.springframework.beans.TypeMismatchException”,
"message": "无法将 'java.lang.String' 类型的值转换为 所需类型“java.lang.Long”;嵌套异常是 java.lang.NumberFormatException:对于输入字符串:\"-26.2041028\"",
"路径": "/fetchlocation"
我猜这是因为当我进行 GET 调用时,rest API 将坐标作为字符串而不是 Longs 接收。如何确保调用时其余 API 获取的是 Longs 而不是 String 值?
当我更改其余 API 方法以将字符串作为参数时,我可以轻松地使该方法工作,但对于类型检查,我更喜欢使用 Longs。
public void fetchlocation(@RequestParam String lat, @RequestParam String lng, @RequestParam String radius){ //fetches location}
【问题讨论】:
-
数字不是 Long 而是 Float/Double。由于浮点数无法转换为 Long(从最初收到的字符串)
-
我想您可能想使用
Double之类的东西作为坐标值。否则,您的位置将失去 很多 的精度。 (如果您成功地将坐标四舍五入到最接近的整数,那么每个方向的误差范围将约为 +/- 35 英里。)
标签: java spring rest spring-mvc