简体   繁体   中英

Getting java.lang.IllegalStateException: No thread-bound request found when using Kafka Consumer to hit API

My requirement is to fetch cookie from the API requests and then pass on the same cookie to subsequent requests in order to maintain session using the same JsessionID.

For that I am trying to fetch cookie using HttpHeaders by Autowiring HttpServletRequests. If I hit an API using Controller I don't get any exception and I am able to fetch all the request attributes but if I run Kafka and API runs through Kafka Consumer, I am getting below 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.

  @Autowired
  HttpServletRequest req;
  
  @Autowired
  HttpServletResponse res;
 
 public ResponseBean putData() {

    String jsessionId = null;
    String id = null;

    // Creating Headers for request
    String authValue = req.getHeader("authorization");
    String headerCookie = req.getHeader("Cookie");


    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.set("authorization", authValue);
    httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    httpHeaders.set(HttpHeaders.COOKIE, headerCookie);


    HttpEntity httpEntity = new HttpEntity(httpHeaders);

    ResponseEntity<String> responseEntity = restTemplate.exchange
            ("https://url", HttpMethod.POST, httpEntity, String.class);
    jsessionId = responseEntity.getHeaders().getFirst("Set-Cookie");


    if (jsessionId !=null && jsessionId.contains("JSESSION")) {
        logger.info("New login session created");
        id = jsessionId.substring(jsessionId.indexOf('=') + 1, jsessionId.indexOf(';'));
       
        Cookie cookie = new Cookie("JSESSIONID", id);
        res.addCookie(cookie);
    }

}

PS- I am not using SpringSecurity.

Can anyone please help me in fixing the above issue.

java.lang.IllegalStateException: No thread-bound request found:

This exception is trying to tell you that there is no current request being processed by the HttpServletRequest that has been auto-wired into your class.

Something is calling the putData() method but not as through a call to the HTTP controller. You mention that you are using the "Kafka Consumer" so maybe it is the consumer code which is calling putData() ? You can't call it because the method needs to have a web request context to work since it needs the request headers and cookie information.

You should probably separate out the data persistence code so that both the controller putData() method and the kafka consumer can both call it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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