[英]How do I force the HTTP Request Connector to use a certain Content-Type?
[英]Force feign to use a specific content-type
我正在使用 Feign 在遠程 Java 接口上進行 REST 調用。 接口定義如下:
@Produces({"text/xml", "application/json"})
由於JAXRSContract
簡單發送data.template().header("Accept", serverProduces);
服務器選擇用xml
有效載荷回答。
在這種情況下,有什么辦法可以強制 Feign 請求 `JSON 有效負載?
您可以使用攔截器覆蓋標頭:
Feign.builder().setInterceptor(requestTemplate -> {
Map<String, Collection<String>> map = new HashMap<>(requestTemplate.headers());
map.put("Accept", Collections.singleton("application/json"));
requestTemplate.headers(null);
requestTemplate.headers(map);
});
方法requestTemplate.headers(map)
看起來設計得很糟糕,如果你傳遞了一個有效的 map,它的值會被添加到內部 header map; 如果您通過null
而不是內部 header map 被重置。 這就是為什么你需要調用它兩次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.