Search code examples
springspring-bootspring-restcontroller

Getting HttpServerErrorException: 500 null when using @PathVariable


I am not getting this. I have a @RestController which is supposed to handle requests like /foo?latitude=15.12345. Of course there should be more parameters, but it's not even working for one.

This is the controller:

@RestController
public class GooglePlaceController {

    private final static Logger LOGGER = LogManager.getLogger(GooglePlaceController.class);

    @Autowired
    private GooglePlaceService googlePlaceService;

    @RequestMapping(value = "/foo", method = RequestMethod.GET)
    public List<GooglePlaceEntity> doNearbySearch(@PathVariable Float latitude) {
        LOGGER.trace("Searching for places nearby.");
        return null;
    }

}

and this is the request I am building:

public ResponseEntity<List<PlaceDto>> nearbySearch(Float lat, Float lng, Integer radius, Boolean googleSearch) {

    String href = UriComponentsBuilder.fromHttpUrl("http://localhost:8081/foo")
            .queryParam("latitude", lat)
            .build().encode().toString();

    ResponseEntity<Object> forEntity = this.oauthRestTemplate.getForEntity(href, null, Object.class);

    return null;
}

However, I am getting this exception below, unless I remove @PathVariable Float latitude in which case the request gets handled correctly.

org.springframework.web.client.HttpServerErrorException: 500 null
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:97)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79)
    at org.springframework.security.oauth2.client.http.OAuth2ErrorHandler.handleError(OAuth2ErrorHandler.java:84)
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:777)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:730)
    at org.springframework.security.oauth2.client.OAuth2RestTemplate.doExecute(OAuth2RestTemplate.java:128)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:686)
    at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:361)
    at mz.api.client.Client.nearbySearch(Client.java:191)

But here is the thing:

In another controller I am having this:

@RequestMapping(value = "/groups/{groupId}/places", method = RequestMethod.GET)
public List<PlaceEntity> getGooglePlaces(@PathVariable Long groupId) {
    return this.userGroupService.getPlaces(groupId);
}

@RequestMapping(value = "/groups/{groupId}/google-places", method = RequestMethod.POST)
public void addGooglePlace(@PathVariable Long groupId, @RequestParam String googlePlaceId) {
    this.userGroupService.addGooglePlace(groupId, googlePlaceId);
}

and those requests are working without any problems.


Solution

  • @PathVariable is for parameters which are part of the URL like: /groups/{groupId}/google-places.

    If parameter is after the ? you should use @RequestParam annotation.

    @RequestMapping(value = "/foo", method = RequestMethod.GET)
    public List<GooglePlaceEntity> doNearbySearch(@RequestParam Float latitude) {
        LOGGER.trace("Searching for places nearby.");
        return null;
    }