Spring RestTemplate带参数的GET请求

390

我需要进行一个包含自定义头信息和查询参数的 REST 调用。我只设置了带有头信息(没有主体)的 HttpEntity,并使用以下方式的 RestTemplate.exchange() 方法:

HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");

Map<String, String> params = new HashMap<String, String>();
params.put("msisdn", msisdn);
params.put("email", email);
params.put("clientVersion", clientVersion);
params.put("clientType", clientType);
params.put("issuerName", issuerName);
params.put("applicationName", applicationName);

HttpEntity entity = new HttpEntity(headers);

HttpEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params);

使用调度程序servlet无法解析请求到处理程序,导致客户端失败。通过调试,发现似乎没有发送请求参数。

当我使用一个请求正文和没有查询参数的POST进行交换时,它可以正常工作。

有人有任何想法吗?

17个回答

3
public static void main(String[] args) {
         HttpHeaders httpHeaders = new HttpHeaders();
         httpHeaders.set("Accept", MediaType.APPLICATION_JSON_VALUE);
         final String url = "https://host:port/contract/{code}";
         Map<String, String> params = new HashMap<String, String>();
         params.put("code", "123456");
         HttpEntity<?> httpEntity  = new HttpEntity<>(httpHeaders); 
         RestTemplate restTemplate  = new RestTemplate();
         restTemplate.exchange(url, HttpMethod.GET, httpEntity,String.class, params);
    }

3
我提供了一个RestTemplate GET方法的代码片段,其中包含路径参数示例。
public ResponseEntity<String> getName(int id) {
    final String url = "http://localhost:8080/springrestexample/employee/name?id={id}";
    Map<String, String> params = new HashMap<String, String>();
    params.put("id", id);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity request = new HttpEntity(headers);
    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, String.class, params);
    return response;
}

2
您可以使用以下代码处理字符串。
URL_EXAMPLE="http://{domain}/Index.php?Username={user}&password={password}";

String domain = "example.com";
String user = "user";
String password = "password";

String data=this.restTemplate.getForObject(URL_EXAMPLE,String.class,domain,user,password);

2

另一种方法的解决方案:

private String execute(String url, Map<String, String> params) {
    UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(url)
    // predefined params
            .queryParam("user", "userValue")
            .queryParam("password", "passwordValue");
    params.forEach(uriBuilder::queryParam);
    HttpHeaders headers = new HttpHeaders() {{
        setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        setAccept(List.of(MediaType.APPLICATION_JSON));
    }};
    ResponseEntity<String> request = restTemplate.exchange(uriBuilder.toUriString(), 
                HttpMethod.GET, new HttpEntity<>(headers), String.class);
    return request.getBody();

}

1
如果您的URL是http://localhost:8080/context path?msisdn={msisdn}&email={email},那么
Map<String,Object> queryParams=new HashMap<>();
queryParams.put("msisdn",your value)
queryParams.put("email",your value)

根据您描述的方式,适用于RestTemplate交换方法


0

嗨,我使用以下代码构建带有查询参数的URL:

UriComponentsBuilder.fromHttpUrl(url)
                .queryParam("bikerPhoneNumber", "phoneNumberString")
                .toUriString();

0

我发现两件事情,查询参数(@RequestParam)或动态URL(@PathVariable)可以通过两种方式传递,即:

  1. 将数据作为数组传递。
  2. 使用映射。

在这两种模式中,有一件事是共同的。 需要动态传递数据,并且我们可以使用花括号{}标记这些位置。 例如:

https://api.themoviedb.org/3/movie/now_playing?api_key={api_key}

Spring 文档 现在我们需要传递数据,

样式 1

Map<String,String> data=new LinkedHashMap<String,String>();
data.put("api_key", api_key);
template.getForObject(url, TMDB.class,data);

样式2

Object[] arr= {api_key};
template.getForObject(url, TMDB.class,arr);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接