Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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}
like image 312
Simon Avatar asked Sep 20 '15 12:09

Simon


1 Answers

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.

like image 154
Aditya Singh Avatar answered Oct 29 '22 00:10

Aditya Singh