spring-boot - 如何使用resttemplate对url进行编码,在查询参数中包含特殊字符
问题描述
使用 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 工作正常。
解决方案
您只需要添加 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
推荐阅读
- node.js - Strapi Webpack 卡在 Docker 容器中
- javascript - 如何更新每个项目的时间
- reactjs - 如何将函数作为道具传递给 React Custom Modal?
- java - 在 JavaFX 中禁用 ImageView 抗锯齿
- jdbc - 在mybatismy中查询TDengine如果没有数据会报错
- flutter - 参数类型“Function”不能分配给参数类型“void Function()?
- php - 如何在 laravel 在其 web.php 中检查它之前重定向 url?
- html - 如何修复导航栏和侧边栏在向下滚动时粘在一起,HTML & CSS
- function - kotlin 的 compareTo 函数是否支持泛型或者我做错了什么?
- android - Spotify android 应用程序如何保存当前的音乐播放时间?就像您退出应用程序并在前几天打开它一样?