關于feign對x-www-form-urlencode類型的encode和decode問題
對x-www-form-urlencode類型的encode和decode問題
記錄一下開發(fā)過程中遇到的一個問題。
問題場景
使用feign調用另一服務b時,在feign-client包里跑單測能調用成功,在另一項目a引入該feign-client時使用同樣的參數(shù)調用失敗。content-type為application/x-www-form-urlencode POST請求
問題原因
入?yún)⒅杏幸粋€String,數(shù)據(jù)是jsonArray,包含","和":",在打印請求的參數(shù)發(fā)現(xiàn),feign-client包里對參數(shù)encode之后,“,” 和“:"不變,而項目a調用feign-client對參數(shù)encode會把“,” 和“:"encode成%2C和%3A,導致服務b decode失敗。
后來debug對比兩次的不同點,發(fā)現(xiàn)關鍵點在于feign中生成的RequestTemplate不同;一步一步調試發(fā)現(xiàn),feign-client包中 feign-core版本是10.2.3,項目a的feign-core版本是9.5.1,兩者在生成RequestTemplate中底層對參數(shù)encode的方法不同,低版本使用的JDK1.8的URLEncode,高版本使用的feign里的UriUtils.encodeReserved。
feign.template.UriUtils.encodeReserved對參數(shù)編碼時,會將參數(shù)列表中key-value的value分割為byte數(shù)組,然后依次對每個byte進行encode,根據(jù)isAllowed方法判斷是否需要encode,pctEncode(b, encoded)方法是真正去encode的地方。下面的代碼可以看到UriUtils.encodeReserved保留了字母數(shù)字逗號冒號等字符。而java.net.URLEncode的encode方法不會保留逗號冒號等字符。
private static String encodeChunk(String value, FragmentType type, Charset charset) { byte[] data = value.getBytes(charset); ByteArrayOutputStream encoded = new ByteArrayOutputStream(); // 依次對每個byte編碼 for (byte b : data) { // 對于一些字符不進行編碼 if (type.isAllowed(b)) { encoded.write(b); } else { /* percent encode the byte */ pctEncode(b, encoded); } } return new String(encoded.toByteArray()); } boolean isAllowed(int c) { return this.isPchar(c) || (c == '/'); } protected boolean isPchar(int c) { return this.isUnreserved(c) || this.isSubDelimiter(c) || c == ':' || c == '@'; } protected boolean isUnreserved(int c) { return this.isAlpha(c) || this.isDigit(c) || c == '-' || c == '.' || c == '_' || c == '~'; } protected boolean isAlpha(int c) { return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'); } protected boolean isDigit(int c) { return (c >= '0' && c <= '9'); } protected boolean isSubDelimiter(int c) { return (c == '!') || (c == '$') || (c == '&') || (c == '\'') || (c == '(') || (c == ')') || (c == '*') || (c == '+') || (c == ',') || (c == ';') || (c == '='); }
至于為什么服務b對URLEncode編碼的參數(shù)解析不了,還待探索,因為我沒看服務b的decode代碼,不知道服務b是怎么解析的。
由于服務b已經(jīng)對多方提供,不能讓他們適應低版本去增加解決方案(事實上他們也不想動代碼),所以只能從發(fā)起方來解決問題。
可能的解決辦法(沒來得及嘗試)
1、版本升級,將項目a的feign-core版本升級到10.2.3,問題能解決(已嘗試),但是項目a中已經(jīng)使用低版本的feign與多個服務交互,雖然理論上feign會向下兼容,但是我不敢輕易升級版本,而且版本號跨度還挺大,風險太大 = =。
2、將高版本的encode方法提取出來,手動配置到feign.encode中
3、加一個interceptor,將低版本encode的template再特殊decode一次,保持和高版本的一致 (失敗,template屬性是unModifiable)
4、看能否讓項目a調用b服務時使用高版本feign-core ,其他feign仍然使用低版本
5、放棄feign 用 httpclient調用 。。。。
附:feign的調用棧
1、 ReflectiveFeign 被反射實例化
2、SynchronousMethodHandler.invoke
2-1、先實例化RequestTemplate 此處encode參數(shù)
2-2、executeAndDecode方法,將RequestTemplate build為request,此處會先執(zhí)行攔截器
2-3、execute 執(zhí)行 訪問原程服務
2-4、將response decode
附上源碼:
// 2、SynchronousMethodHandler.invoke public Object invoke(Object[] argv) throws Throwable { // 2-1、先實例化RequestTemplate 此處encode參數(shù) RequestTemplate template = buildTemplateFromArgs.create(argv); Retryer retryer = this.retryer.clone(); while (true) { try { return executeAndDecode(template); } catch (RetryableException e) { try { retryer.continueOrPropagate(e); } catch (RetryableException th) { Throwable cause = th.getCause(); if (propagationPolicy == UNWRAP && cause != null) { throw cause; } else { throw th; } } if (logLevel != Logger.Level.NONE) { logger.logRetry(metadata.configKey(), logLevel); } continue; } } } Object executeAndDecode(RequestTemplate template) throws Throwable { // 2-2、executeAndDecode方法,將RequestTemplate build為request Request request = targetRequest(template); if (logLevel != Logger.Level.NONE) { logger.logRequest(metadata.configKey(), logLevel, request); } Response response; long start = System.nanoTime(); try { // 2-3、execute 執(zhí)行 訪問原程服務 response = client.execute(request, options); } catch (IOException e) { if (logLevel != Logger.Level.NONE) { logger.logIOException(metadata.configKey(), logLevel, e, elapsedTime(start)); } throw errorExecuting(request, e); } long elapsedTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); boolean shouldClose = true; try { if (logLevel != Logger.Level.NONE) { response = logger.logAndRebufferResponse(metadata.configKey(), logLevel, response, elapsedTime); } if (Response.class == metadata.returnType()) { if (response.body() == null) { return response; } if (response.body().length() == null || response.body().length() > MAX_RESPONSE_BUFFER_SIZE) { shouldClose = false; return response; } // Ensure the response body is disconnected byte[] bodyData = Util.toByteArray(response.body().asInputStream()); return response.toBuilder().body(bodyData).build(); } if (response.status() >= 200 && response.status() < 300) { if (void.class == metadata.returnType()) { return null; } else { Object result = decode(response); shouldClose = closeAfterDecode; return result; } } else if (decode404 && response.status() == 404 && void.class != metadata.returnType()) { Object result = decode(response); shouldClose = closeAfterDecode; return result; } else { throw errorDecoder.decode(metadata.configKey(), response); } } catch (IOException e) { if (logLevel != Logger.Level.NONE) { logger.logIOException(metadata.configKey(), logLevel, e, elapsedTime); } throw errorReading(request, response, e); } finally { if (shouldClose) { ensureClosed(response.body()); } } } long elapsedTime(long start) { return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); } Request targetRequest(RequestTemplate template) { // 此處會先執(zhí)行攔截器 for (RequestInterceptor interceptor : requestInterceptors) { interceptor.apply(template); } return target.apply(template); } Object decode(Response response) throws Throwable { try { // 2-4、將response decode return decoder.decode(response, metadata.returnType()); } catch (FeignException e) { throw e; } catch (RuntimeException e) { throw new DecodeException(response.status(), e.getMessage(), e); } }
feign x-www-form-urlencoded 類型請求
spring發(fā)送 content-type=application/x-www-form-urlencoded 和普通請求不太一樣。
試了好多方式,最后用以下方式成功
@FeignClient( ?? ?name = "ocr-api", ?? ?url = "${orc.idcard-url}", ?? ?fallbackFactory = OcrClientFallbackFactory.class ) public interface OcrClient { ? ?? ?@PostMapping( ?? ??? ?value = "/v1/demo/idcard", ?? ??? ?headers = {"content-type=application/x-www-form-urlencoded"} ?? ?) ?? ?OcrBaseResponse<IdCardResponse> getIdCarInfo(@RequestBody MultiValueMap<String, Object> request); }
Post請求,參數(shù)使用@RequestBody 并且使用 MultiValueMap。
? ? // 測試代碼 ?? ?@Resource ?? ?private OcrClient ocrClient; ?? ?@GetMapping("getIdCardInfo") ?? ?public Message getIdCardInfo() { ?? ??? ?MultiValueMap<String, Object> req = new LinkedMultiValueMap<>(); ?? ??? ?req.add("request_id", 12343531123L); ?? ??? ?req.add("img_url", "xxx.jpg"); ?? ??? ?req.add("source", -1); ?? ??? ?req.add("out_business_id", 1321434234L); ?? ??? ?OcrBaseResponse<IdCardResponse> idCarInfo = ocrClient.getIdCarInfo(req); ?? ??? ?return Message.success(idCarInfo); ?? ?}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring mvc是如何實現(xiàn)與數(shù)據(jù)庫的前后端的連接操作的?
今天給大家?guī)淼氖顷P于Spring mvc的相關知識,文章圍繞著Spring mvc是如何實現(xiàn)與數(shù)據(jù)庫的前后端的連接操作的展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06Java利用StringBuffer替換特殊字符的方法實現(xiàn)
這篇文章主要介紹了Java利用StringBuffer替換特殊字符的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04IntelliJ IDEA右鍵文件夾沒有Java Class文件的原因及解決方法
這篇文章主要介紹了IntelliJ IDEA右鍵文件夾沒有Java Class文件的原因及解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09springboot?serviceImpl初始化注入對象實現(xiàn)方式
這篇文章主要介紹了springboot?serviceImpl初始化注入對象實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05SpringBoot使用Validation校驗參數(shù)的詳細過程
這篇文章主要介紹了SpringBoot使用Validation校驗參數(shù),本文結合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09