java - 当rest模板具有使用Spring boot 1.5.x的拦截器时,我们可以使用@RestClientTest吗?

标签 java spring spring-boot spring-mvc spring-rest

我正在使用 Spring Boot 1.5.x (Spring 4.2.x),并且创建了一个 RestClientSdk spring 组件类,如下所示:

@Component
public class RestClientSdkImpl implements RestClientSdk {

    private RestTemplate restTemplate;

    @Autowired
    public RestClientSdkImpl(RestTemplateBuilder restTemplateBuilder) {
        this.restTemplate = restTemplateBuilder.build();
    }
    ...
    //other methods kept out for brevity
}

我还定义了一个 DefaultRestTemplateCustomizer spring 组件,如下所示:

@Component
public class DefaultRestTemplateCustomizer implements RestTemplateCustomizer {

    private LogClientHttpRequestInterceptor logClientHttpRequestInterceptor;

    @Autowired
    public DefaultRestTemplateCustomizer(LogClientHttpRequestInterceptor logClientHttpRequestInterceptor) {
        this.logClientHttpRequestInterceptor = logClientHttpRequestInterceptor;
    }

    @Override
    public void customize(RestTemplate restTemplate) {
        restTemplate.getInterceptors().add(logClientHttpRequestInterceptor);
        restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
    }

}

这样,我定义了一个测试类,如下所示,它使用 @RestClientTest 注释,如下所示。

@RunWith(SpringRunner.class)
@RestClientTest(RestClientSdk.class)
@ActiveProfiles("test")
/*
 * The RestClientTest only includes the most minimal configuration to include a rest template builder, 
 * so we include the rest sdk auto config within the scope of the test 
 */ 
@ImportAutoConfiguration(RestSdkAutoConfiguration.class)
public class RestClientApplicationBehaviourTest{

    @Autowired
    private RestClientSdk restClientSdk;

    @Autowired
    private MockRestServiceServer mockRestServiceServer;

    /**
     * A simple Http Get that retrieves a JSON document from a rest server and 
     * produces a plain old java object as a response.
     */ 
    @Test
    public void testPlainHttpGet() throws IOException{
        //ARRANGE
        RestClientDto<?> simpleGetRequest = simpleHttpGet();
        mockRestServiceServer.expect(once(), requestTo("http://localhost:8080/account/1234567890"))
                                   .andRespond(withSuccess(IOUtils.getInputAsString("/stubs/account.json"),MediaType.APPLICATION_JSON));
        //ACT
        Account account = restClientSdk.send(simpleGetRequest, Account.class);
        //ASSERT
        mockRestServiceServer.verify();     
        Assert.assertNotNull(account);
        Assert.assertNotNull(account.getAccountId());
        Assert.assertNotNull(account.getFirstName());
        Assert.assertNotNull(account.getLastName());
    }
...
//not including other methods for brevity 
}
<小时/>

问题

<小时/>

由于 MockRestServiceServer 构建器使用 MockClientHttpRequestFactory 覆盖我的其余模板中的 BufferingClientHttpRequestFactory,因此我从正文中收到 null 响应。这是因为日志记录拦截器正在读取来自响应的输入流,因此该流不再有可读取的内容。 BufferingClientHttpRequestFactory 会阻止这种情况发生。现在,我知道从 Spring 5.0.5 开始,MockRestServiceServer 构建器中有一个名为 bufferContent 的额外选项,但我没有迁移到 Spring 5.x 的选项(Spring Boot 2 .x),所以我想知道是否有办法使用 Spring Boot 1.5.x/Spring 4.2.x 进行配置。

我先谢谢你了!

胡安

最佳答案

为了解决该问题,我需要定义一个测试配置,这将允许我覆盖客户端请求工厂。请参阅下面的代码。这有点 hacky,但我认为真正的解决方案是升级到 Spring 5.x/Spring Boot 2.x。

@Configuration
@Profile("test")
public class MockRestServiceServerConfiguration {

    /**
     * Wrap the Mock Rest client factory in the buffered one.  
     * @param restClientSdk The rest client SDK containing the rest template to use.
     * @return The mock rest service server to use.
     */
    @Bean
    public MockRestServiceServer mockRestServiceServer(RestClientSdkImpl restClientSdkImpl) {
        RestTemplate restTemplate = restClientSdkImpl.getRestTemplate();
        MockRestServiceServer server = MockRestServiceServer.createServer(restTemplate);

        //need to do this because getRequestFactory returns InterceptingHttpRequestFactory wraping the mock rest service server request factory
        List<ClientHttpRequestInterceptor> templateInterceptors = restTemplate.getInterceptors();
        restTemplate.setInterceptors(null);

        //now we wrap the delegate, which should be the mock rest service server request factory 
        BufferingClientHttpRequestFactory bufferingFact = new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory());
        restTemplate.setRequestFactory(bufferingFact);
        restTemplate.setInterceptors(templateInterceptors);
        return server;
    }
}

关于java - 当rest模板具有使用Spring boot 1.5.x的拦截器时,我们可以使用@RestClientTest吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61038912/

相关文章:

java - 具有多线程的 REST Api,用于在 Spring Boot 中处理文件

java - 越来越严重 : Could not locate SessionFactory in JNDI while getting sessionfactory

java - JPanel观察者

java - Java 应用程序中的 SameSite cookie

java - 为自定义 userdetailsservice 定义 bean

java - Spring批处理在xml读取错误后不继续处理记录

java - 当客户端调用 html 或 jsp 页面时,是否可以选择登录凭据的窗口

java - 在方法级别而不是类级别的基本 Jax-RS PATH 配置

spring-boot - spring-kafka KafkaListener 运行时自动启动行为

java - 如何在使用 Spring Boot 时通过在浏览器中点击 URL 来加载 Angular 组件?