-
Notifications
You must be signed in to change notification settings - Fork 38.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HttpHeaders cause NPE in use with RestTemplate #22871
Comments
Yes it should not result in an NPE but the cause is likely a bug that should be addressed. What is the header data in such a case? Maybe that can help to reproduce it. Also any chance you could retry with 5.2.0.BULID-SNAPSHOT to confirm if it is related to #22821. |
The problem is: it only happens every other day, by chance. So if I'd be using the snapshot (which is not that easy as I'm totally inheriting from spring-boot release), I still can't be sure if it is fixed. Unfortunately I did not find a way to reproduce the issue at will. Maybe it has to do with the fact the I'm sending and processing my requests in an async future? @Autowired
private ThreadPoolExecutor executor;
List<Request> requests;
//the basic idea is to prepare all requests required, then dispatch all req async and collect the responses also async. then wait until all responses have been received.
List<CompletableFuture<Response>> futures = requests
.stream()
.map(req -> CompletableFuture.supplyAsync(
() -> restTemplate.postForEntity(URL, req, Response.class).getBody(), executor)
.collect(Collectors.toList());
List<Response> responses = futures.stream().map(CompletableFuture::join).collect(Collectors.toList()); I also found another log entry, that interestingly throws the NPE also inside an async process. @Service
@Async
public class MyHeaderInterceptor() {
public void traceHeaders(ClientHttpResponse rsp) {
LOGGER.info(message.getHeaders().toString());
}
} Same issue: occasionally getting NPEs like:
To be complete, my @Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
HttpClient httpClient = HttpClientBuilder.create()
.setMaxConnTotal(200)
.setMaxConnPerRoute(100)
.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
factory.setConnectTimeout(2000);
factory.setReadTimeout(10000);
return builder.requestFactory(() -> new BufferingClientHttpRequestFactory(factory)).build();
} |
We didn't get any new reports for this, I'm assuming this has been fixed with #22821. |
Occasionally I'm getting NPEs wehn using
RestTemplate.postForObject()
. I could not manually reproduce the issues, as I don't know the case when the headers "seem" to be null.All I know is that the
ClientHttpResponse
is not null in those cases and correctly filled with header+body data. The next request with the exact same parameters then works again.Maybe this has to do with #22821, because
new HttpHeaders()
is initialized withLinkedCaseInsensitiveMap
?Anyways I think adding (or getting) the
HttpHeaders
should never result in an NPE, this should be caught somewhere down the stack.The text was updated successfully, but these errors were encountered: