Using RestTemplate in Spring. Exception- Not enough variables available to expand

前端 未结 6 1133
走了就别回头了
走了就别回头了 2020-11-29 05:29

I am trying to access the contents of an API and I need to send a URL using RestTemplate.

String url1 = \"http://api.example.com/Search?key=52ddafbe3ee659bad         


        
6条回答
  •  渐次进展
    2020-11-29 06:06

    If the solution suggested by sotirios-delimanolis is a little difficult to implement in a scenario, and if the URI string containing curly braces and other characters is guaranteed to be correct, it might be simpler to pass the encoded URI string to a method of RestTemplate that hits the ReST server.

    The URI string can be built using UriComponentsBuilder.build(), encoded using UriComponents.encode(), and sent using RestTemplate.exchange() like this:

    public ResponseEntity requestRestServer()
    {
        HttpEntity entity = new HttpEntity<>(requestHeaders);
        UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(rawValidUrl)
                .queryParams(
                        (LinkedMultiValueMap) allRequestParams);
        UriComponents uriComponents = builder.build().encode();
        ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,
                entity, String.class);
        return responseEntity;
    }
    
    
    

    Building, encoding, and extracting URI have been seperated out for clarity in the above code snippet.

    提交回复
    热议问题