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 03:31:56 UTC

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

sunjinghan7331 opened a new issue #1503: 使用restTemplate调用provider接口时如何设置请求头信息
URL: https://github.com/apache/servicecomb-java-chassis/issues/1503
 
 
   consumer在使用restTemplate调用provider接口时,如何设置request header的信息,如常用的Content-Type,或者自定义的 Authorization等信息?

----------------------------------------------------------------
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

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

Posted by GitBox <gi...@apache.org>.
liubao68 commented on issue #1503: 使用restTemplate调用provider接口时如何设置请求头信息
URL: https://github.com/apache/servicecomb-java-chassis/issues/1503#issuecomment-569593804
 
 
   你可以试试 @afan321 的方法,采用HttpEnetity传递header。 但是需要注意,这个限制了底层通信使用REST(即HTTP).  更通用的方式是采用 [Context](https://docs.servicecomb.io/java-chassis/zh_CN/general-development/context.html), 可以参考 [fence](https://github.com/apache/servicecomb-fence) 认证鉴权的实现.

----------------------------------------------------------------
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

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

Posted by GitBox <gi...@apache.org>.
sunjinghan7331 edited a comment on issue #1503: 使用restTemplate调用provider接口时如何设置请求头信息
URL: https://github.com/apache/servicecomb-java-chassis/issues/1503#issuecomment-569602813
 
 
   > 我写过的一个方法 你可参考下 /**
   > * @param url 请求地址
   > * @param bodyParam 请求体
   > * @param headerParam 头部信息
   > * @param rClass 返回体类型
   > * @param 请求体类型
   > * @param 返回体类型
   > * @return 返回结果
   > */
   > public <T, R> R doPost(String url, T bodyParam, Map<String, String> headerParam, Class 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());
   > }
   > ```
   
   我使用了你的方法,可以得到正确的返回数据,但是我无法获得这些数据。详细说明一下,在我的provider工程中,我以
   TenantResponse tenant = new TenantResponse();
   tenant.setUsername("username");
   tenant.setPassword("password");
   return Response.ok().entity(tenant).build();
   作为我的接口返回值,Response是javax.ws.rs.core中的类。
   
   在我的consumer中调用此接口,
   String token = "Basic tl3JfTTNKkii42th4b/IjA==";
           HttpHeaders requestHeaders = new HttpHeaders();
           requestHeaders.add("Authorization", token);
           HttpEntity entity = new HttpEntity(requestHeaders);
           ResponseEntity<TenantResponse> response = restTemplate.exchange("cse://user-mgmt-be/v1/users/userId", HttpMethod.GET, entity, TenantResponse.class);
   System.out.prinln(response .getBody().getUsername());
   
   由于consumer与provider是两个不同的微服务,所以TenantResponse类是在两个微服务中分别定义的,但是具有同样的字段。不过在response.getBody()时提示ClassCastException,provider的TenantResponse不能转化为consumer的TenantResponse,请问应该如何解决?

----------------------------------------------------------------
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

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

Posted by GitBox <gi...@apache.org>.
afan321 commented on issue #1503: 使用restTemplate调用provider接口时如何设置请求头信息
URL: https://github.com/apache/servicecomb-java-chassis/issues/1503#issuecomment-569615702
 
 
   遇见过这个问题 好像是这个版本的CSE对这参数类型有强校验  所以 ResponseEntity<Object> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, Object.class) 我用object去接返回值,然后再转化成想要的类型

----------------------------------------------------------------
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

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

Posted by GitBox <gi...@apache.org>.
sunjinghan7331 commented on issue #1503: 使用restTemplate调用provider接口时如何设置请求头信息
URL: https://github.com/apache/servicecomb-java-chassis/issues/1503#issuecomment-569602813
 
 
   > 我写过的一个方法 你可参考下 /**
   > * @param url 请求地址
   > * @param bodyParam 请求体
   > * @param headerParam 头部信息
   > * @param rClass 返回体类型
   > * @param 请求体类型
   > * @param 返回体类型
   > * @return 返回结果
   > */
   > public <T, R> R doPost(String url, T bodyParam, Map<String, String> headerParam, Class 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());
   > }
   > ```
   
   我使用了你的方法,可以得到正确的返回数据,但是我无法获得这些数据。详细说明一下,在我的provider工程中,我以
    `TenantResponse tenant = new TenantResponse();
   tenant.setUsername("username");
   tenant.setPassword("password");
   return Response.ok().entity(tenant).build();`
   作为我的接口返回值,Response是javax.ws.rs.core中的类。
   
   在我的consumer中调用此接口,
   `String token = "Basic tl3JfTTNKkii42th4b/IjA==";
           HttpHeaders requestHeaders = new HttpHeaders();
           requestHeaders.add("Authorization", token);
           HttpEntity entity = new HttpEntity(requestHeaders);
           ResponseEntity<TenantResponse> response = restTemplate.exchange("cse://user-mgmt-be/v1/users/userId", HttpMethod.GET, entity, TenantResponse.class);
   System.out.prinln(response .getBody().getUsername());`
   
   由于consumer与provider是两个不同的微服务,所以TenantResponse类是在两个微服务中分别定义的,但是具有同样的字段。不过在response.getBody()时提示ClassCastException,provider的TenantResponse不能转化为consumer的TenantResponse,请问应该如何解决?

----------------------------------------------------------------
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

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

Posted by GitBox <gi...@apache.org>.
yhs0092 commented on issue #1503: 使用restTemplate调用provider接口时如何设置请求头信息
URL: https://github.com/apache/servicecomb-java-chassis/issues/1503#issuecomment-569617251
 
 
   > 
   > 
   > 遇见过这个问题 好像是这个版本的CSE对这参数类型有强校验 所以 ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, Object.class) 我用object去接返回值,然后再转化成想要的类型
   
   强类型版本的Java-Chassis(1.x版本)会对RestTemplate的返回值类型有严格校验。要求返回值类型的包名和类名跟provider端服务契约声明的返回值类型完全一致(类型信息记录在契约的`x-java-class`属性里)。

----------------------------------------------------------------
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

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

Posted by GitBox <gi...@apache.org>.
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

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

Posted by GitBox <gi...@apache.org>.
sunjinghan7331 closed issue #1503: 使用restTemplate调用provider接口时如何设置请求头信息
URL: https://github.com/apache/servicecomb-java-chassis/issues/1503
 
 
   

----------------------------------------------------------------
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

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

Posted by GitBox <gi...@apache.org>.
sunjinghan7331 commented on issue #1503: 使用restTemplate调用provider接口时如何设置请求头信息
URL: https://github.com/apache/servicecomb-java-chassis/issues/1503#issuecomment-569600857
 
 
   > 你可以试试 @afan321 的方法,采用HttpEnetity传递header。 但是需要注意,这个限制了底层通信使用REST(即HTTP). 更通用的方式是采用 [Context](https://docs.servicecomb.io/java-chassis/zh_CN/general-development/context.html), 可以参考 [fence](https://github.com/apache/servicecomb-fence) 认证鉴权的实现.
   
   好的,谢谢

----------------------------------------------------------------
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

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

Posted by GitBox <gi...@apache.org>.
liubao68 commented on issue #1503: 使用restTemplate调用provider接口时如何设置请求头信息
URL: https://github.com/apache/servicecomb-java-chassis/issues/1503#issuecomment-569841100
 
 
   2.0版本会支持这种使用方式。 

----------------------------------------------------------------
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