Spring NumberFormatException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long
Asked Answered
A

3

10

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:

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"

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?

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.

public void fetchlocation(@RequestParam String lat, @RequestParam String lng, @RequestParam String radius){ //fetches location}
Alfons answered 20/9, 2015 at 12:32 Comment(2)
The number is not a Long but rather a Float/Double. Because of the floating point the number cannot be converted to Long (from the string it is received originally)Suzansuzann
I imagine you'd want to use something like a Double for coordinate values. Otherwise you're going to lose a lot of precision in your location. (If you succeed in your effort to round coordinates to the nearest whole number then you're going to have a margin of error of approximately +/- 35 miles in each direction.)Weatherman
A
8

The number you are trying to convert to java.lang.Long is -26.2041028.

Your number contains a .(decimal). This is not allowed for Longs. You need to use 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.

Like, -26.2041028D for Double.

Anastigmatic answered 20/9, 2015 at 13:6 Comment(4)
Thanks - this is working now, can't believe I missed that. Once I made it a double, the rest api call worked. Thanks!Alfons
Lol, I have never used any frameworks. But I have experience of writing core Java code since 2 years. 1 tip I want to give to all framework users, whenever you get an error that you are too much pissed off with, just think of all the basics that u've learnt in your bigenner Java class. :) Happy Coding!Anastigmatic
The D isn't required or recommended.Unselfish
The note about D makes no sense here (even though I prefer it too) - there is a static initialization anywhere in the question code, the only "double" value comes from the query parameter.Spalla
M
1

It seems that you might be using a GET request instead of a POST request.

Magnanimous answered 15/10, 2023 at 10:22 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Tanny
S
-1

You need to make sure that the String that you're converting to Long contains only numbers. That means for example a string like this, "10d" isn't convertible to a Long, hence the exception.

Scutellation answered 20/9, 2015 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.