Springboot restTemplate请求资源时 设置请求头

 以下不好使。不知道为啥

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

Map params = new HashMap();
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 response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params);

 换成下面的就ok:

HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
        .queryParam("msisdn", msisdn)
        .queryParam("email", email)
        .queryParam("clientVersion", clientVersion)
        .queryParam("clientType", clientType)
        .queryParam("issuerName", issuerName)
        .queryParam("applicationName", applicationName);

HttpEntity entity = new HttpEntity<>(headers);

HttpEntity response = restTemplate.exchange(
        builder.build().encode().toUri(), 
        HttpMethod.GET, 
        entity, 
        String.class);

你可能感兴趣的:(springboot)