You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TestRestTemplate PUT and POST requests not working when adding Netty as a dependency #7240
When adding Netty as Dependency (compile 'io.netty:netty-all:4.1.6.Final') to my Spring Boot project my tests using TestRestTemplate.postForObject(...) and TestRestTemplate.put(...) aren't working anymore. I get the following error:
Failed to read HTTP message:... HttpMessageNotReadableException: Required request body is missing
Using TestRestTemplate.getForObject(...) works fine. RestTemplate.postForObject(...) and RestTemplate.put(...) are also working fine so I use those as a workaround for the moment.
You can see that by removing the comments in the build.gradle file from the line compile 'org.springframework.boot:spring-boot-starter-data-cassandra'
That's why I added the Netty dependency to my project in the first place.
When adding Netty as dependency compile 'io.netty:netty-all:4.1.6.Final'
to resolve the error above, TestRestTemplate.postForObject(...) fails with the error message
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: ...
So it looks like the error is caused by spring-boot-starter-data-cassandra
It appears to be a problem with Spring Framework's Netty4ClientHttpRequestFactory. When Netty's on the classpath, TestRestTemplate will automatically use Netty4ClientHttpRequestFactory. If you set the request factory on RestTemplate it has the same problem.
The JavaDoc for TestRestTemplate says that it can be configured by providing a RestTemplateBuilder@Bean. So I added one to a static inner @TestConfiguration class and set the request factory to SimpleClientHttpRequestFactory. I injected the RestTemplateBuilder into the test class so I can also use it to build my normal RestTemplate.
This works just fine without the dependency on Netty. But when I add the dependencies BOTH tests fail. If the problem was the Netty4ClientHttpRequestFactory this shouldn't happen. Isn't that odd?
I updated the sample accordingly. Maybe you want to have a look.
Each method on the builder returns a new instance so the code that you had was returning the builder in its default form. This meant that both tests were now be using the Netty client.
Your method that configures the builder should look like this:
Activity
wilkinsona commentedon Oct 28, 2016
@eseidinger Can you please provide a small sample that reproduces the problem?
eseidinger commentedon Oct 28, 2016
@wilkinsona I created a little project on GitHub to isolate the problem. It's just one
RestControllerwith one request handler method and two test methods, one usingRestTemplateand one usingTestRestTemplate.See https://github.com/eseidinger/spring-boot-netty-problem
It's actually not Netty alone causing the error. Adding the
spring-boot-starter-data-cassandradependency I get the following error running the test.You can see that by removing the comments in the build.gradle file from the line
compile 'org.springframework.boot:spring-boot-starter-data-cassandra'That's why I added the Netty dependency to my project in the first place.
When adding Netty as dependency
compile 'io.netty:netty-all:4.1.6.Final'to resolve the error above,
TestRestTemplate.postForObject(...)fails with the error messageSo it looks like the error is caused by
spring-boot-starter-data-cassandrawilkinsona commentedon Oct 28, 2016
Thanks for the sample.
It appears to be a problem with Spring Framework's
Netty4ClientHttpRequestFactory. When Netty's on the classpath,TestRestTemplatewill automatically useNetty4ClientHttpRequestFactory. If you set the request factory onRestTemplateit has the same problem.wilkinsona commentedon Oct 28, 2016
Here's a couple of stripped down tests that don't use Spring Boot at all and illustrate the problem:
@eseidinger Unfortunately, I think this needs to be addressed by the Spring Framework team (or perhaps the Netty team). Can you please open an SPR Jira ticket (https://jira.spring.io/browse/SPR) in the first instance?
eseidinger commentedon Oct 28, 2016
I wrote this before I saw that you closed this issue but maybe you find it interesting anyway...
@wilkinsona Thanks for your feedback.
The JavaDoc for
TestRestTemplatesays that it can be configured by providing aRestTemplateBuilder@Bean. So I added one to a static inner@TestConfigurationclass and set the request factory toSimpleClientHttpRequestFactory. I injected theRestTemplateBuilderinto the test class so I can also use it to build my normalRestTemplate.This works just fine without the dependency on Netty. But when I add the dependencies BOTH tests fail. If the problem was the
Netty4ClientHttpRequestFactorythis shouldn't happen. Isn't that odd?I updated the sample accordingly. Maybe you want to have a look.
wilkinsona commentedon Oct 28, 2016
Each method on the builder returns a new instance so the code that you had was returning the builder in its default form. This meant that both tests were now be using the Netty client.
Your method that configures the builder should look like this:
eseidinger commentedon Oct 29, 2016
@wilkinsona Thanks for your help. The workaround with the
SimpleClientHttpRequestFactoryis working now. I created an issue on JIRA:https://jira.spring.io/browse/SPR-14860