【问题标题】:How to encode url, containing special characters in query parameters using resttemplate如何使用resttemplate对url进行编码,在查询参数中包含特殊字符
【发布时间】:2020-08-15 18:11:20
【问题描述】:

当我使用 RestTemplate 访问在查询参数中包含特殊字符的 URL 时出现错误。

我的网址是这样的

 https://someendpoint?q=countryCode:(AB+yz)&q=type.code:12345&q=banner.code:A1&q=openDate[*+TO+NOW%2B3MONTH]%20&q=!date:[*+TO+NOW]

有人可以帮助我如何使用 RestTemplate 对这种类型的 URL 进行编码。 当我通过浏览器或邮递员点击时,此 URL 工作正常。

【问题讨论】:

    标签: spring-boot resttemplate urlencode spring-resttemplate


    【解决方案1】:

    您只需要添加 StringHttpMessageConverter。

      RestTemplate template = new RestTemplate();
      template.getMessageConverters()
        .add(0, new 
    StringHttpMessageConverter(Charset.forName("UTF-8")));
     ResponseEntity<Object> response = template.exchange(endpoint, method, entity, 
                                                    Object.class
    

    【讨论】:

    • 它仍然给出错误。它说 500 内部服务器错误(很可能是 url 编码错误。)
    • @AamirSheraz: 你试过这个 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url) .queryParam("msisdn", msisdn) .queryParam("email", email) .queryParam("clientVersion", clientVersion ) .queryParam("clientType", clientType) .queryParam("issuerName", issuerName) .queryParam("applicationName", applicationName);
    猜你喜欢
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多