Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 712
Raj Avatar asked Mar 17 '15 10:03

Raj


People also ask

How do you handle exceptions in spring boot REST API?

Altogether, the most common way is to use @ExceptionHandler on methods of @ControllerAdvice classes so that the exception handling will be applied globally or to a subset of controllers. ControllerAdvice is an annotation introduced in Spring 3.2, and as the name suggests, is “Advice” for multiple controllers.

What are the HTTP methods that Cannot be implemented in spring boot REST service?

Return 405 (Method Not Allowed), unless you want to replace every resource in the entire collection of resource - use with caution. 404 (Not Found) - if id not found or invalid and creating new resource is not allowed. Return 405 (Method Not Allowed), unless you want to modify the collection itself..

How do I send a custom error message in REST API spring boot?

The most basic way of returning an error message from a REST API is to use the @ResponseStatus annotation. We can add the error message in the annotation's reason field. Although we can only return a generic error message, we can specify exception-specific error messages.


2 Answers

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
like image 113
Bond - Java Bond Avatar answered Oct 11 '22 21:10

Bond - Java Bond


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

  • Spring MVC @PathVariable getting truncated

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:

  • https://jira.spring.io/browse/SPR-6164
like image 42
Stephen C Avatar answered Oct 11 '22 22:10

Stephen C