获取HttpServerErrorException:使用@PathVariable时为500 null

时间:2018-04-12 17:11:20

标签: spring spring-boot spring-restcontroller

我没有得到这个。我有一个@RestController,它应该处理像/foo?latitude=15.12345这样的请求。当然应该有更多参数,但它甚至不适用于一个

这是控制器:

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

这些请求没有任何问题。

1 个答案:

答案 0 :(得分:1)

@PathVariable适用于以下网址的参数:/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;
}