0

I'm developing my first REST API using Spring boot. I created multiple requests on my API and I'm currently testing them. I found a bug in one of these requests : When the @PathVariable is set it's rounded

Here is the code :

@RequestMapping(path = "/find/near/{lati}/{longi}", method = GET, produces = APPLICATION_JSON_VALUE)
    public Animals getAnimalsNearPosition(@PathVariable double lati, @PathVariable double longi) throws CenterNotFoundException {
        // On récupère le centre associé à cette cage
        Center center = centers.findCenterNear(lati, longi);

        Animals animals = new Animals();

        // On recherche parmis toutes les cages celles ayant une position proche
        for (Cage c : center.getCages()) {
            if (Position.isNear(c.getPosition().getLatitude(), lati, c.getPosition().getLongitude(), longi)) {
                for (Animal a : c.getResidents()) {
                    animals.addAnimal(a);
                }
            }
        }

        // On retourne les animaux
        return animals;
    }

For instance if I send GET /find/near/4.50/1.39 the variable lati is equals to 4.50 but longi is equal to 1.0 and not 1.39. I tried to debug using Intellij but I can't figure why it happens...

Any idea ?

Git Harpon
  • 171
  • 1
  • 11

2 Answers2

2

Spring is truncating value after dot (.) Because, the first value is already 4.5, it seems not truncated but actually this first parameter is also truncated. You can find the solve the issue in here also.

spring @pathvariable with dot is getting truncated

Hatice
  • 874
  • 1
  • 8
  • 17
1

I solved it using regex

@RequestMapping(path = "/find/at/{lati}/{longi:.+}", method = GET, produces = APPLICATION_JSON_VALUE)
Git Harpon
  • 171
  • 1
  • 11