简体   繁体   English

如何使用假客户端传递请求参数?

[英]How to pass request param using feign client?

I am currentlt using Feign Client to call an end point to get outlook mails.我目前正在使用 Feign Client 调用端点以获取 outlook 封邮件。 But the request parameter are not passing correctly in the api.但是请求参数在 api 中没有正确传递。

@FeignClient(name = "email", url = "${BASE.URI}")
public interface EmailClient {

    @GetMapping("/mailfolders/Inbox/messages")
    EmailRequestNew getMessages(@RequestHeader HashMap<String, Object> headers,
                                @RequestParam String filter);

Through service I am calling this Email client to get Mails and passing the filter as below where from and to are datetime通过服务,我正在调用这个 Email 客户端来获取邮件并传递过滤器,如下所示,从哪里到哪里是日期时间

String param = "$filter=receivedDateTime ge " + from + " and receivedDateTime lt " + to +
        "&$expand=singleValueExtendedProperties($filter=id+eq+'String+0x0070')";

but the actual api which are calling is not accurate assume BASE.URI is something like (10.0.0.120:8080)但实际调用的 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

but I want my complete api to be like below when I hardcoded the Request param in the 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')"))但是当我在 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')

How can I achive this.我怎样才能做到这一点。

I tried URL Encoding/Decoding but it is not working.我尝试了 URL 编码/解码,但它不起作用。

Example:例子:

URLDecoder.decode(param,"UTF-8")

UriUtils.encodePath(param, "UTF-8"); UriUtils.encodePath(参数, "UTF-8");

But nothing is working.但是没有任何效果。

So I was able to do this by creating a RequestInterceptor and then decoding the URI and also change my EmailClient to take PathVariable instead of RequestParam.所以我能够通过创建 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);
        }
    }
}

This is the final uri which is created:这是创建的最终 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.

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