简体   繁体   English

将Netty添加为依赖项时,Spring TestRestTemplate POST和PUT无法正常工作

[英]Spring TestRestTemplate POST and PUT not working when adding Netty as a dependency

When adding 添加时

    compile 'io.netty:netty-all:4.1.6.Final'

as a dependency to my Spring Boot project all my unit tests using TestRestTemplate.postForObject(...) and TestRestTemplate.put(...) start to fail. 作为对Spring Boot项目的依赖,我所有使用TestRestTemplate.postForObject(...)TestRestTemplate.put(...)单元测试都开始失败。 It seems the request body isn't transmitted because I get the following error 似乎未发送请求正文,因为我收到以下错误

Failed to read HTTP message:... HttpMessageNotReadableException: Required request body is missing

The service is still working and accepts POST and PUT requests. 该服务仍在工作,并接受POST和PUT请求。 I checked that using curl. 我用卷发检查了一下。 TestRestTemplate.getForObject(...) on the other hand is still working. TestRestTemplate.getForObject(...)仍在工作。 Using a normal RestTemplate for POST and PUT is also working. 将普通的RestTemplate用于POST和PUT也可以使用。

I tried to configure the TestRestTemplate ( right solution, wrong implementation -> see my answer ) 我试图配置TestRestTemplate( 正确的解决方案,错误的实现->参见我的答案

@TestConfiguration static class TestConfig { @Bean RestTemplateBuilder restTemplateBuilder() { RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder() restTemplateBuilder.detectRequestFactory(false) restTemplateBuilder.requestFactory(new SimpleClientHttpRequestFactory()) return restTemplateBuilder } } @TestConfiguration静态类TestConfig {@Bean RestTemplateBuilder restTemplateBuilder(){RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder()restTemplateBuilder.detectRequestFactory(false)restTemplateBuilder.requestFactory(new SimpleClientHttpRequestFactory())return restTemplateBuilder}}

with no effect. 没有任何效果。 I also tried to use Netty4ClientHttpRequestFactory which didn't work either. 我也尝试使用也Netty4ClientHttpRequestFactory I tried to configure the HttpMessageConverter using the GsonHttpMessageConverter for example... no effect. 例如,我尝试使用GsonHttpMessageConverter配置HttpMessageConverter ...无效。

TestRestTemplate isn't working because Spring Boot configures it to use Netty4ClientHttpRequestFactory when it finds Netty on the classpath and the Netty4ClientHttpRequestFactory doesn't seem to work. TestRestTemplate不起作用,因为当Spring Boot在类路径上找到Netty并且Netty4ClientHttpRequestFactory似乎Netty4ClientHttpRequestFactory时,Spring Boot会将其配置为使用Netty4ClientHttpRequestFactory

See https://github.com/spring-projects/spring-boot/issues/7240 and https://jira.spring.io/browse/SPR-14860 参见https://github.com/spring-projects/spring-boot/issues/7240https://jira.spring.io/browse/SPR-14860

To make TestRestTemplate.postForObject(...) and TestRestTemplate.put(...) I had to provide a RestTemplateBuilder @Bean and configure it to use SimpleClientHttpRequestFactory . 为了制作TestRestTemplate.postForObject(...)TestRestTemplate.put(...)我必须提供RestTemplateBuilder @Bean并将其配置为使用SimpleClientHttpRequestFactory The RestTemplateBuilder @Bean in the question has a few errors. 问题中的RestTemplateBuilder @Bean有一些错误。 It's supposed to look like this 应该看起来像这样

@TestConfiguration
static class TestConfig {
    @Bean
    public RestTemplateBuilder restTemplateBuilder() {
        return new RestTemplateBuilder().detectRequestFactory(false).requestFactory(SimpleClientHttpRequestFactory.class);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM