小编Ira*_* Re的帖子

如何测试Spring WebClient何时重试?

我需要实现以下行为:

  • 发出 REST 发布请求
  • 如果响应返回状态为429 Too many requests,则最多重试 3 次,延迟 1 秒
  • 如果第三次重试失败或发生任何其他错误,请记录并向数据库写入内容
  • 如果请求成功(http status 200),记录一些信息

我想使用 Spring WebClient 来实现此目的,并提出了以下代码:

Mono<ClientResponse> response = webClient.post()
            .uri(URI.create("/myuri"))
            .body(BodyInserters.fromObject(request))
            .retrieve()
            .onStatus(httpStatus -> httpStatus.equals(HttpStatus.TOO_MANY_REQUESTS), 
                      response -> Mono.error(new TooManyRequestsException("System is overloaded")))
            .bodyToMono(ClientResponse.class)
            .retryWhen(Retry.anyOf(TooManyRequestsException.class)
                                          .fixedBackoff(Duration.ofSeconds(1)).retryMax(3))
            .doOnError(throwable -> saveToDB(some_id, throwable))
            .subscribe(response -> logResponse(some_id, response));
Run Code Online (Sandbox Code Playgroud)

现在我想测试重试机制和错误处理是否按我的预期工作。也许我可以使用StepVerifier来达到此目的,但我只是不知道如何在我的情况下使用它。有什么有用的提示吗?

reactive-programming rx-java project-reactor spring-webclient

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

将可选参数传递给Spring Data Repository方法的@Query

我在当前项目中使用Spring Data和Neo4j并处于以下情况:

@RestController
@RequestMapping(value = SearchResource.URI)
public class PersonResource {

public static final String URI = "/person";

@Autowired
PersonRepository personRepository;

@GetMapping
public Collection<Person> findPersons(
    @RequestParam(value = "name", required = false) String name,
    @RequestParam(value = "birthDate", required = false) Long birthDate,
    @RequestParam(value = "town", required = false) Spring town) {

    Collection<Person> persons;

    if (name != null && birthDate == null && town == null) {
        persons = personRepository.findPersonByName(name);
    } else if (name != null && birthDate != null && town == …
Run Code Online (Sandbox Code Playgroud)

spring software-design cypher spring-data spring-data-neo4j

5
推荐指数
1
解决办法
4403
查看次数

未捕获的TypeError:d3.schemeCategory20不是函数

我是d3js和所有javascript世界的新手.在我的html文件中,我只需导入脚本:

<script src="https://d3js.org/d3.v4.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

通过尝试使用以下内容:

var ordinalColorScale = d3.schemeCategory20();
Run Code Online (Sandbox Code Playgroud)

我得到了例外

未捕获的TypeError:d3.schemeCategory20不是index.html上的函数:48

我是否需要任何其他必须导入的d3js模块?或者可能导致问题的原因是什么?

javascript d3.js

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

如何将 spark 数据集转换为 scala seq

我有以下案例类

case class Station(id: Long, name: String) extends Node
Run Code Online (Sandbox Code Playgroud)

和一个站的 Spark 数据集

vertices: org.apache.spark.sql.Dataset[Station] = [id: bigint, name: string]
Run Code Online (Sandbox Code Playgroud)

我想将顶点数据集转换为 Seq[Station]。我找到了很多关于如何从序列创建数据集的教程,反之亦然。你对我有什么暗示吗?

scala scala-collections apache-spark apache-spark-dataset

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