在改造动态路径路径、动态

由网友(伊人回眸泪倾城)分享简介:我试图访​​问资源与像 http://192.168.1.64:5050/api/ {} API_KEY /updater.info 。I'm trying to access a resource with like http://192.168.1.64:5050/api/{api_key}/updater.in...

我试图访​​问资源与像 http://192.168.1.64:5050/api/ {} API_KEY /updater.info

I'm trying to access a resource with like http://192.168.1.64:5050/api/{api_key}/updater.info.

我将如何动态地设置 API_KEY 参数?我用尝试了 RequestInterceptor 没有成功,其中的基本URL为 http://192.168.1.64:5050/api/ {API_KEY}

How would I dynamically set the api_key parameter? I've tried using a RequestInterceptor without success where the base url is http://192.168.1.64:5050/api/{api_key}.

@Override
public void intercept(RequestFacade request) {
    request.addPathParam("api_key", apiKey);
}

还有没有其他办法?

Are there any other alternatives?

推荐答案

路径置换不发生的API端点,只能在方法的相对URL字符串的基本URL中。我会假设你不希望preFIX相对网址上你的每一个接口方法的声明。

Path replacement does not happen inside the base URL of the API endpoint, only the relative URL string on the method. I'm going to assume you don't want to prefix the relative URLs on every one of your interface method declarations.

虽然措辞不当,的javadoc的端点 规定:

While poorly worded, the javadoc of Endpoint states:

调用者应该随时咨询最新的值的实例,而不是缓存返回的值。

Callers should always consult the instance for the latest values rather than caching the returned values.

这意味着,每一个要求端点实例将被征询的基础URL的值。

This means that for every request the Endpoint instance will be consulted for the value of the base URL.

您可以提供自定义端点的实施上,您可以更改API密钥值:

You can supply a custom Endpoint implementation on which you can change the API key value:

public final class FooEndpoint implements Endpoint {
  private static final String BASE = "http://192.168.1.64:5050/api/";

  private String url;

  public void setApiKey(String apiKey) {
    url = BASE + apiKey;
  }

  @Override public String getName() {
    return "default";
  }

  @Override public String getUrl() {
    if (url == null) throw new IllegalStateException("API key not set.");
    return url;
  }
}
阅读全文

相关推荐

最新文章