使用@PathVariable 时获取 HttpServerErrorException: 500 null

Getting HttpServerErrorException: 500 null when using @PathVariable

我没听懂。我有一个 @RestController 应该可以处理像 /foo?latitude=15.12345 这样的请求。当然应该有更多参数,但它甚至对 one.

不起作用

这是控制器:

@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;
    }

}

这是我正在构建的请求:

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;
}

但是,我在下面收到此异常,除非我删除 @PathVariable Float latitude,在这种情况下请求得到正确处理

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)

但事情是这样的:

在另一个控制器中我有这个:

@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);
}

并且这些请求没有任何问题。

@PathVariable 用于属于 URL 的参数,例如:/groups/{groupId}/google-places.

如果参数在 ? 之后,您应该使用 @RequestParam 注释。

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