简体   繁体   English

如何在伪客户端(尤其是HTTP标头)中传递标头?

[英]How to pass header in feign client especially HTTP Headers?

I tried with Rest Template passing http headers and its working as expected but some how it not working with feign client. 我尝试了Rest Template传递http标头及其按预期方式工作,但有些方法不适用于伪装客户端。

Rest Client code:- 其余客户端代码:-

import org.springframework.http.HttpHeaders;

RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeader = new HttpHeaders();
httpHeader.set("appsecret-proof", header);
HttpEntity<?> request = new HttpEntity<>(httpHeader);
String url = "https://localhost/groups/{pathVariable}/members/";
Map<String, String> map = new HashMap<>();
map.put("pathVariable", pathVariable);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
                                                   .queryParam("token", token);
String uriBuilder = builder.build().toString();

ResponseEntity<MemberObject> responseEntity = restTemplate.exchange(uriBuilder, HttpMethod.POST, request,MemberObject.class, map);

MemberObject memberObject = null;
if (responseEntity != null && responseEntity.getBody() != null) {
    memberObject = responseEntity.getBody();
}

Fegin Client Code:- 特定客户代码:-

import org.springframework.web.bind.annotation.RequestHeader;

@PostMapping(value = "/groups/?token=xyz")
MemberObject getMemberGroup(@RequestHeader("appsecret-proof") String appsecretProof);

But fegin client giving an error as : 但是fegin客户给出的错误为:

{"error":{"message":"Authentication credentials could not be found.","type":"Authentication Error","code":404,"sub_code":0}}.

Hardcoded the token in feign client still its giving an same error. 在伪装客户端中对令牌进行硬编码仍然会产生相同的错误。 It might be error with headers....any suggestion how to setup header in feign client. 标头可能是错误的。...有关在伪装客户端中设置标头的任何建议。

That's how you should structure the call if you're using Feign: 如果您使用的是Feign,则应采用以下方式构造调用:

import feign.Headers;
import feign.Param;
import feign.RequestLine;
...
@RequestLine("POST /groups/?token={appSecretProof}")
@Headers("Content-Type: application/json")
MemberObject getMemberGroup(@Param("appSecretProof") String appSecretProof);

In the above snippet the value of appSecretProof is substituted into the URL at execution time as the corresponding value of the token . 在以上代码段中,在执行时将appSecretProof的值替换为URL作为令牌的相应值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM