简体   繁体   中英

How to pass request param using feign client?

I am currentlt using Feign Client to call an end point to get outlook mails. But the request parameter are not passing correctly in the 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

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)

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')"))

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.

Example:

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

UriUtils.encodePath(param, "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.


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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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