[英]How to pass request param using feign client?
我目前正在使用 Feign Client 調用端點以獲取 outlook 封郵件。 但是請求參數在 api 中沒有正確傳遞。
@FeignClient(name = "email", url = "${BASE.URI}")
public interface EmailClient {
@GetMapping("/mailfolders/Inbox/messages")
EmailRequestNew getMessages(@RequestHeader HashMap<String, Object> headers,
@RequestParam String filter);
通過服務,我正在調用這個 Email 客戶端來獲取郵件並傳遞過濾器,如下所示,從哪里到哪里是日期時間
String param = "$filter=receivedDateTime ge " + from + " and receivedDateTime lt " + to +
"&$expand=singleValueExtendedProperties($filter=id+eq+'String+0x0070')";
但實際調用的 api 不准確假設 BASE.URI 類似於 (10.0.0.120:8080)
https://BASE.URI/mailfolders/Inbox/messages?param=%24filter%3DreceivedDateTime%20ge%202022-11-18T05%3A32%3A56Z%20and%20receivedDateTime%20lt%202022-11-18T09%3A32%3A56Z%26%24expand%3DsingleValueExtendedProperties%28%24filter%3Did%20eq%20%27String%200x0070%27%29
但是當我在 GetMapping (@GetMapping("/mailfolders/Inbox/messages$filter=receivedDateTime ge 2022-11-18T05:32:56Z and receivedDateTime lt 2022-11) -18T09:32:56Z&$expand=singleValueExtendedProperties($filter=id+eq+'String+0x0070')"))
https://dev-api.bhspecialty.com/xchange/v1/mailfolders/Inbox/messages?%24filter=receivedDateTime%20ge%202022-11-18T04:16:58Z%20and%20receivedDateTime%20lt%202022-11-18T08:16:58Z&%24expand=singleValueExtendedProperties($filter=id+eq+'String+0x0070')
我怎樣才能做到這一點。
我嘗試了 URL 編碼/解碼,但它不起作用。
例子:
URLDecoder.decode(param,"UTF-8")
UriUtils.encodePath(參數, "UTF-8");
但是沒有任何效果。
所以我能夠通過創建 RequestInterceptor 然后解碼 URI 並更改我的 EmailClient 以采用 PathVariable 而不是 RequestParam 來做到這一點。
@GetMapping(value = "/mailfolders/Inbox/messages?$expand={expand}&$filter={filter}", consumes = MediaType.APPLICATION_JSON_VALUE)
EmailRequestNew getMessages(@RequestHeader HashMap<String, Object> headers,
@PathVariable String filter, @PathVariable String expand);
@Component
public class FeignClientRequestInterceptor implements RequestInterceptor {
private static Logger logger = LogManager.getLogger(FeignClientRequestInterceptor.class);
@Override
public void apply(RequestTemplate template) {
try {
template.uri(URLDecoder.decode(template.request().url(), "UTF-8"));
logger.info("FeignClientRequestInterceptor: " + URLDecoder.decode(template.request().url(), "UTF-8") );
} catch (UnsupportedEncodingException e) {
logger.log(Level.INFO,"Error in FeignClientRequestInterceptor: " + template.request().url() );
throw new RuntimeException(e);
}
}
}
這是創建的最終 uri:
https://BASE.URI/mailfolders/Inbox/messages?%24expand=singleValueExtendedProperties($filter=id%20eq%20'String%200x0070')&%24filter=receivedDateTime%20ge%202022-11-21T08:17:59Z%20and%20receivedDateTime%20lt%202022-11-21T12:17:59Z
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.