You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2019/12/30 05:41:54 UTC

[GitHub] [servicecomb-java-chassis] afan321 commented on issue #1503: 使用restTemplate调用provider接口时如何设置请求头信息

afan321 commented on issue #1503: 使用restTemplate调用provider接口时如何设置请求头信息
URL: https://github.com/apache/servicecomb-java-chassis/issues/1503#issuecomment-569589358
 
 
   我写过的一个方法 你可参考下    /**
        * @param url 请求地址
        * @param bodyParam 请求体
        * @param headerParam 头部信息
        * @param rClass 返回体类型
        * @param <T> 请求体类型
        * @param <R> 返回体类型
        * @return 返回结果
        */
       public <T, R> R doPost(String url, T bodyParam, Map<String, String> headerParam, Class<R> rClass) {
   
           if (null == rClass || StringUtils.isEmpty(url)) {
               throw new ExecuteFailedResponse("url is empty or restTemplate is null.");
           }
           HttpHeaders requestHeaders = new HttpHeaders();
           if (null != headerParam) {
               headerParam.entrySet().forEach(entrySet -> requestHeaders.add(entrySet.getKey(), entrySet.getValue()));
           }
           HttpEntity<T> requestEntity = new HttpEntity<T>(bodyParam, requestHeaders);
           ResponseEntity<Object> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, Object.class);
   
           return transforObjet(rClass, response.getBody());
       }

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services