java - Spring Cloud Contract - 如何在 .yaml 文件中传递 "+"(加号)作为查询参数

标签 java yaml spring-cloud

当我尝试以不同方式传递“+”号作为查询参数的一部分时,它会对任何结果进行编码。

我希望传递带有 +380999418260 值的 phone 参数。

尝试了最期望的正确方法:

request:
  method: GET
  url: /customers/prepaid
  queryParameters:
    phone: "%2B380999418260"
  headers:
    Content-Type: application/x-www-form-urlencoded

还尝试直接从 url 传递:

request:
  method: GET
  url: /customers/prepaid?phone=%2B380999418260
  headers:
    Content-Type: application/x-www-form-urlencoded

两者都说手机是%2B380999418260

从日志中,它转换%符号:

"X-Query-String":"phone=%252B380999418260"

我还尝试以非编码方式传递它:

request:
  method: GET
  url: /customers/prepaid
  queryParameters:
    phone: "+380999418260"
  headers:
    Content-Type: application/x-www-form-urlencoded

但在本例中,它将 + 号转换为空格字符。

当我使用此 uri /customers/prepaid?phone=%2B380999418260 从 Postman 访问此端点时,它会返回正确的响应,其中使用带有“+”号的正确电话。

最佳答案

根据this question ,你不能直接传递 + 符号,它应该被编码。但在服务器端,您可以使用 URLDecoder 解码接收到的字符串。类:

String myString = "%2B380999418260";
URLDecoder.decode(myString, "ISO-8859-1");
// output: +380999418260

关于java - Spring Cloud Contract - 如何在 .yaml 文件中传递 "+"(加号)作为查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56090110/

相关文章:

Java:需要添加用户自定义骰子数量的骰子

java - 如何使用查询语法在 Lucene 中跨多个字段进行搜索?

python - 如何使用dictionary/json在yaml中添加 '-'?

Java try..catch 和finally

C++ YAML : How to edit/write to a node in a . yaml文件

kubernetes - 如何将相同的 RBAC 角色分配给两个不同的 IAM 角色以访问 EKS 中的集群?

spring-boot - 使用 Spring Boot 和 Netflix Zuul 的简单反向代理

java - Spring Cloud 配置将文件中的数组抛出 NPE 为空

java - 假装: Retry depending on response status

java - 在 JavaFX 中使用 CSS 时遇到问题