@FeignClient(
contextId = "logFeignClientApi",
value = "log-service",
url = "${chunmi.cloud.service.common.log.url:https://staging-gateway.tokit.com/log-service}",
configuration = {FeignClientProperties.FeignClientConfiguration.class},
fallback = LogFeignClientHystrix.class
)
public interface LogFeignClientApi {
@PostMapping(
value = {"rpc/log/save"},
consumes = {"application/json;charset=UTF-8"},
produces = {"application/json;charset=UTF-8"}
)
Wrapper<?> save(@RequestBody Log log);
}
@Bean
public RequestInterceptor requestInterceptor() {
return requestTemplate -> {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
String method = requestTemplate.method();
String path = requestTemplate.url();
log.debug("url: {}", path);
String appId = "202007190935004";
//同步到请求中
String signature = CryptUtil.getSignature(appId, method, path);
requestTemplate.header(SIGNATURE, signature);
requestTemplate.header(APP_ID, appId);
};
}
when i request the url https://staging-gateway.tokit.com/log-service/rpc/log/save, i put a signature to the request header.
but i can not get the correct url. the correct url is /log-service/rpc/log/save,current is rpc/log/save,how can i get the correct url,thanks
Comment From: cbezmen
Hey @Kimmyzhao ,
I think you are using the wrong field in the FeignClient annotation. Instead of using value = "log-service", you should use path = "log-service"
You can check the java doc.
https://github.com/spring-cloud/spring-cloud-openfeign/blob/main/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClient.java#L112
Comment From: OlgaMaciaszek
Hello, @Kimmyzhao. Please take a look at @cbezmen's suggestion - it should solve the issue.
Comment From: Kimmyzhao
when i use path = "log-service",requestTemplate.path() is rpc/log/save, not the /log-service/rpc/log/save
Comment From: cbezmen
Hey @Kimmyzhao I get the whole path. You can check this repository.
Feign Client Example:
@FeignClient(name = "book", contextId = "bookClient", path = "/customPrefix")
public interface BookClient {
@GetMapping("/fetch-books")
Book getBooks();
}
Interceptor Example:
@Configuration
@Slf4j
public class BookInterceptor {
@Bean
public RequestInterceptor requestInterceptor() {
return requestTemplate -> {
String method = requestTemplate.method();
String url = requestTemplate.feignTarget().url();
String path = requestTemplate.path();
log.info("method: {}", method);
log.info("url: {}", path);
log.info("full url: {}{}", url, path);
};
}
}
Logs:
Comment From: Kimmyzhao
Hey @Kimmyzhao I get the whole path. You can check this repository.
Feign Client Example:
```java @FeignClient(name = "book", contextId = "bookClient", path = "/customPrefix") public interface BookClient {
@GetMapping("/fetch-books") Book getBooks();} ```
Interceptor Example:
```java @Configuration @Slf4j public class BookInterceptor {
@Bean public RequestInterceptor requestInterceptor() { return requestTemplate -> { String method = requestTemplate.method(); String url = requestTemplate.feignTarget().url(); String path = requestTemplate.path(); log.info("method: {}", method); log.info("url: {}", path); log.info("full url: {}{}", url, path); }; }} ```
Logs:
thanks,In this way, the problem has been resolved
Comment From: cbezmen
Nice 🕺🏻