Unable to get dual data type in Rest API developed using Spring

In my REST API, which is developed using the Spring Framework, I have one Rest endpoint that takes two double values, calling Rest: http: // localhost: 8080 / restapp / events / nearby / 12.910967 / 77.599570

here, the first parameter (double data type), i.e. 12.910967, I can correctly get ie, 12.910967. But the second parameter ie, 77.599570, I can only get 77.0 data after truncating the decimal point.

my REST Backend:

@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 to get double data type in REST api?

+6
source share
3 answers

Update your code as shown below. Pay attention to {lngi:.+} , Which indicates a regular expression, meaning that some characters appear after .

 @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 
+2
source

I think this may be the same problem as here:

It was reported that something was trying to apply a suffix match to the incoming URL ... and that absorbed everything after the first dot in the final component of the path.

In fact, this behavior was considered a bug and was fixed in Spring 3.1:

+1
source

You can try to force the entire value with a regular expression in your RequestMapping value:

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

Source: https://habr.com/ru/post/983905/


All Articles