【问题标题】:Spring MVC can not decode query parameter value from restTemplateSpring MVC 无法从 restTemplate 解码查询参数值
【发布时间】:2021-11-08 20:23:15
【问题描述】:

我有一个 Spring Boot 应用程序。对于一个端点,

我用招摇来称呼它。我输入的是:“FGluxxdw==”,然后 swagger 将自动编码为“FGluxxdw%3D%3D”,但端点会自动将值解码为“FGluxxdw==”,以下是整个 swagger 请求示例:

curl -X GET "http://localhost:8080/order/scroll?size=1&orderItemNumber=XX1468&scrollId=FGluxxdw%3D%3D" 
-H "accept: application/hal+json" 
-H "Authorization: Bearer SOME_VALUE" 
-H "api-key: ABC-123"

但是,如果我使用 restemplate,

this.restTemplate.exchange(uri, GET, httpEntity, PRODUCT.class))

而uri的值为

http://localhost:8080/order/scroll?size=1&orderItemNumber=XX1468&scrollId=FGluxxdw%3D%3D

但是,这次spring boot端点无法解码scrollId。

【问题讨论】:

    标签: resttemplate


    【解决方案1】:

    这个问题是几个月前提出的。把这个留在这里帮助别人。试试:

    1. 使用 UriComponentsBuilder。

    2. 将字符集设置为 UTF_8。

    3. 构建 Uri 而不是 UriString。

      UriComponentsBuilder uriBuilder = UriComponentsBuilder
              .fromHttpUrl("http://localhost:8080/order/scroll")
              .encode(StandardCharsets.UTF_8)
              .queryParam("size", 1)
              .queryParam("orderItemNumber", "XX1468")
              .queryParam("scrollId", "FGluxxdw==");
      URI uri = uriBuilder.build().toUri();
      

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多