Spring启动执行器在autoconfiguremockrestserviceserver中断

w1jd8yoj  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(376)

我有一个类,它使用resttemplatebuilder构建多个resttemplates:

private RestTemplate build(RestTemplateBuilder restTemplateBuilder) {
    return restTemplateBuilder
      .rootUri("http://localhost:8080/rest")
      .build();
}

对于我的测试设置,我使用@autoconfiguremockrestserviceserver并使用mockserverrestemplatecustomizer模拟响应:

mockServerRestTemplateCustomizer.getServer()
  .expect(ExpectedCount.times(2),
        requestToUriTemplate("/some/path/{withParameters}", "withParameters"))
    .andRespond(withSuccess());

当我取消注解 spring-boot-actuator 我的pom中的依赖关系,在另一个场景中失败,并显示以下消息。
应输入:/some/path/parameter
实际值:http://localhost:8080/rest/pos/some/path/带参数
通过mockserverrestemplatecustomizer调试,我注意到 spring-boot-actuator 应用“delegatehttpclientinterceptor”来支持rest模板的内置度量。但是,我在rooturirequestexpectationmanager中发现的以下代码存在问题:

public static RequestExpectationManager forRestTemplate(RestTemplate restTemplate,
        RequestExpectationManager expectationManager) {
    Assert.notNull(restTemplate, "RestTemplate must not be null");
    UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler();
    if (templateHandler instanceof RootUriTemplateHandler) {
        return new RootUriRequestExpectationManager(((RootUriTemplateHandler) templateHandler).getRootUri(),
                expectationManager);
    }
    return expectationManager;
}

因为如上所述 spring-boot-actuator 注册一个“delegatehttpclientinterceptor”,这导致上面的代码无法识别rooturitemplatehandler,因此无法使用requesttouritemplate匹配请求。
我在这里错过了什么来让它工作?

dzjeubhm

dzjeubhm1#

正如安迪威尔金森指出的,这似乎是一个错误在 Spring 启动。我用一个示例项目创建了一个问题。

相关问题