0

After the migration to Spring Boot 3 I'm having errors with @Scheduled jobs that made http request to other microservices with RestTemplate.

When an http request it's made, a custom Interceptor starts:

@Override

    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {

and try to get the current request headers with:

currentRequest = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

inside currentRequestAttributes() the objects

private static final ThreadLocal<RequestAttributes> requestAttributesHolder = new NamedThreadLocal("Request attributes");

private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder = new NamedInheritableThreadLocal("Request context");

are null so throws this exception:

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

                at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) ~[spring-web-6.0.9.jar:6.0.9]

The Rest Config class looks like this:

..

import org.apache.hc.client5.http.classic.HttpClient;

import org.apache.hc.client5.http.impl.classic.HttpClients;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.http.client.ClientHttpRequestInterceptor;

import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;

import org.springframework.util.CollectionUtils;

import org.springframework.web.client.RestTemplate;

..

@Configuration

public class RestConfig {

..

   @Bean

   RestTemplate restTemplate() {

               

     HttpClient httpClient = HttpClients.createDefault();

     RestTemplate restTemplate = new RestTemplate();

     restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));

     List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();

     if (CollectionUtils.isEmpty(interceptors)) {

       interceptors = new ArrayList<>();

     }

     ..
    
     interceptors.add(new HeaderInterceptor(authManager, restTemplate, values));

     restTemplate.setInterceptors(interceptors);

     return restTemplate;

I know that it's not a best practice do http request in scheduled jobs, but i want to understand why before the migration was ok and how to fix this.

I have updated all the dependency and reference to javax to jakarta and as suggested from the Spring Boot migration guide, this is the only part of the app that doesn't work as expected.

Miche
  • 23
  • 1
  • 7

0 Answers0