Raj
Raj

Reputation: 759

Not able receive Double data type in Rest API developed with Spring

In my REST API which is developed using Spring Framework, I have one Rest end point which receive Two Double values, the Rest Call is: http://localhost:8080/restapp/events/nearby/12.910967/77.599570

here, first parameter (double datatype) i.e 12.910967 i'm able receive correctly i.e, 12.910967. But second parameter i.e, 77.599570 i'm able receive only 77.0 the data after decimal point truncating.

my REST Backend is:

@RequestMapping(value = "/nearby/{lat}/{lngi}", method = RequestMethod.GET, produces = "application/json")
public List<Event> getNearByEvents(@PathVariable("lat") Double lat, @PathVariable("lngi") Double lngi, HttpServletResponse response) throws IOException 

how receive double data type in REST api?

Upvotes: 7

Views: 3117

Answers (3)

Stephen C
Stephen C

Reputation: 719346

I think this may be the same problem as is described here:

What was reported there was that something was attempting to apply suffix matching to the incoming URL ... and that was consuming everything after the first dot in the final path component.

In fact, this behaviour was deemed to be a bug, and was fixed in Spring 3.1:

Upvotes: 1

Ammar
Ammar

Reputation: 4024

Update your code as below - Note the {lngi:.+} which specifies a regex meaning some characters will appear post .

@RequestMapping(value = "/nearby/{lat}/{lngi:.+}", method = RequestMethod.GET, produces = "application/json")
public List<Event> getNearByEvents(@PathVariable("lat") Double lat, @PathVariable("lngi") Double lngi, HttpServletResponse response) throws IOException

Upvotes: 3

Fritz Duchardt
Fritz Duchardt

Reputation: 11920

You can try forcing the entire value with regex in your RequestMapping value:

@RequestMapping(value = "/nearby/{lat}/{lngi:\d+\.\d+}")

Upvotes: 0

Related Questions