小编Abh*_*bhi的帖子

Springboot:如何使用 WebClient 而不是 RestTemplate 来执行非阻塞和异步调用

我有一个使用 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 呈现数据流

  1. 控制器接收客户端调用
  2. provider 获取 String 格式的数据
  3. 提供程序处理字符串
  4. 数据提供给控制器

控制器.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)

java spring spring-boot spring-webflux

9
推荐指数
1
解决办法
2万
查看次数

Springboot:通过 Resttemplate 防止 % 的双重编码

我们的代码使用 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 查询参数进行双重编码

java spring-mvc spring-boot

3
推荐指数
1
解决办法
3172
查看次数

Springboot:控制器中的@RequestParam 读取带有“&amp;”和“#”的请求为空

我有一个在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'

java spring spring-mvc urlencode spring-boot

2
推荐指数
1
解决办法
843
查看次数