我有一个使用 Springboot Resttemplate 的 springboot 项目。我们已经从 1.5.3 迁移到 springboot 2.0.1,我们正在尝试使用 WebClient 使其余的调用异步进行。我们曾经使用 Resttemplate 处理收到的字符串,如下所示。但是 WebClient 只返回 Mono 或 Flux 中的数据。如何将数据作为字符串获取。已经尝试了 block() 方法,但它执行异步调用。
@Retryable(maxAttempts = 4, value = java.net.ConnectException.class,
backoff = @Backoff(delay = 3000, multiplier = 2))
public Mono<String> getResponse(String url) {
return webClient.get().uri(urlForCurrent).accept(APPLICATION_JSON)
.retrieve()
.bodyToMono(String.class);
}
Run Code Online (Sandbox Code Playgroud)
使用 RestTemplate 呈现数据流
控制器.java
@RequestMapping(value = traffic/, method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public String getTraffic(@RequestParam("place") String location) throws InterruptedException, ExecutionException {
String trafficJSON = Provider.getTrafficJSON(location)
return …Run Code Online (Sandbox Code Playgroud) 我们的代码使用 Asyncresttemplate 如下
String uri = http://api.host.com/version/test?address=%23&language=en-US&format=json
getAysncRestTemplate().getForEntity(uri, String.class);
Run Code Online (Sandbox Code Playgroud)
但是%23在 Rest 模板中进行了双重编码,%2523并且 url 变为
http://api.host.com/version/test?address=%2523&language=en-US&format=json,但是我需要传递编码字符串,如果我传递解码数据'#',它不会编码
如何在不对 URL 进行双重编码的情况下发送此请求?
已经尝试使用 UriComponentsBuilder 避免使用 Spring 的 RestTemplate 对 URL 查询参数进行双重编码
我有一个在Tomcat上运行的Spring-Boot应用程序。在其中,我有一个带有请求参数的RestController。不读取或作为查询参数传递。例如,如果我给出,则控制器方法不会作为值@RequestParam'&''#'http://localhost:8080/v1/test?query=&&
@RequestMapping(value = "/v1/test", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
public String getV2LocationByName(
@RequestParam
String cityName cityName,
@RequestParam(value = LANGUAGE, defaultValue = US_ENGLISH_LOCALE) String language,
HttpServletRequest request) throws InterruptedException, ExecutionException {
--------------
---------------
System.out.println(cityName);
}
Run Code Online (Sandbox Code Playgroud)
上面的程序为 & 和 # 返回空输出
为什么 spring 限制这些特殊字符,但不限制任何其他特殊字符?并且如果使用查询参数为 'Andaman&Nicobar',则查询参数被读取为 'Andaman'