Che*_*ter 5 java spring spring-webflux
我正在尝试使用 spring Webflux Webclient 发送国际格式的电话号码,并通过另一个也使用 webflux 的应用程序读取该电话号码。
我的代码如下所示:
webClient = WebClient.builder()
.baseUrl(baseUrl)
.build();
return webClient
.get()
.uri(uriBuilder -> uriBuilder
.path("/endpoint")
.queryParam("phone-number", "+33612345678")
.build()
)
.retrieve()
.bodyToMono(String.class);
Run Code Online (Sandbox Code Playgroud)
不幸的是,在这个调用和接收者之间的某个地方,加号被空格替换了。端点接收:“33612345678”作为字符串。
请求的 netty 调试日志显示了以下内容:
+--------+-------------------------------------------------+----------------+
|00000000| 47 45 54 20 2f 63 75 73 74 6f 6d 65 72 73 3f 70 |GET /endpoint?p|
|00000010| 68 6f 6e 65 2d 6e 75 6d 62 65 72 3d 2b 33 33 36 |hone-number=+336|
|00000020| 31 32 33 34 35 36 37 38 26 6f 6e 6c 79 2d 72 65 |12345678
Run Code Online (Sandbox Code Playgroud)
我尝试自己对电话号码进行编码,如下所示:
.queryParam("phone-number", UriUtils.encode("+34612345678", StandardCharsets.UTF_8))
Run Code Online (Sandbox Code Playgroud)
Netty 的日志显示:
+--------+-------------------------------------------------+----------------+
|00000000| 47 45 54 20 2f 63 75 73 74 6f 6d 65 72 73 3f 70 |GET /endpoint?p|
|00000010| 68 6f 6e 65 2d 6e 75 6d 62 65 72 3d 25 32 35 32 |hone-number=%252|
|00000020| 42 33 34 36 31 32 33 34 35 36 37 38 20 48 54 54 |B34612345678 HTT|
Run Code Online (Sandbox Code Playgroud)
看来电话号码已经被编码了两次。
+ -> %2B -> %252B
加号已被编码,UriUtils.encode然后 uriBuilder 已对 % 进行了编码。
我发现让它工作的唯一方法是禁用 UriBuilder 的编码:
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl);
factory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);
this.webClient = WebClient.builder()
.baseUrl(baseUrl)
.uriBuilderFactory(factory)
.build();
Run Code Online (Sandbox Code Playgroud)
并有我的自定义编码UriUtils.encode("+34612345678", StandardCharsets.UTF_8)
在这种情况下,netty 的日志看起来像预期的那样:
+--------+-------------------------------------------------+----------------+
|00000000| 47 45 54 20 2f 63 75 73 74 6f 6d 65 72 73 3f 70 |GET /endpoint?p|
|00000010| 68 6f 6e 65 2d 6e 75 6d 62 65 72 3d 25 32 42 33 |hone-number=%2B3|
|00000020| 34 36 31 32 33 34 35 36 37 38 20 48 54 54 50 2f |4612345678 HTTP/|
Run Code Online (Sandbox Code Playgroud)
当然,接收电话号码的端点得到:“+33612345678”
总而言之,看起来 UriBuilder 正在编码某些符号,如“%”,但没有编码“+”符号。Spring参考:https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#web-uri-encoding
我遇到了同样的问题,但从您链接的 Spring 参考中找到了解决方法。
这应该适合你:
return webClient
.get()
.uri(uriBuilder -> UriComponentsBuilder.fromUri(uriBuilder.build())
.path("/endpoint")
.queryParam("phone-number", "{phone-number}")
.encode()
.buildAndExpand("+33612345678")
.toUri()
)
.retrieve()
.bodyToMono(String.class);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4507 次 |
| 最近记录: |