Three Ways That Help You Retrieve Request Object from Spring-Boot

<p>HttpServletRequest Request for short, is a Servlet API that provides an object for obtaining client-initiated HTTP request information.</p> <p>For example:</p> <ol> <li>Get the request parameters</li> <li>Get the request header</li> <li>Get the session information</li> <li>Get the IP address of the request and other information</li> </ol> <p>So the question is, in Spring Boot, what are the methods to get the Request object?</p> <p>There are three common ways to get Request objects:</p> <ol> <li>Getting the Request object through the request parameters;</li> <li>Getting the Request object through RequestContextHolder;</li> <li>Obtaining a Request object through automatic injection.</li> </ol> <p>Let&rsquo;s explore this a little further.</p> <h2>1 . Getting by request parameter</h2> <pre> @RequestMapping(&quot;/&quot;) @ResponseBody public void index(HttpServletRequest request){ // do something }</pre> <p>The logic behind this method is that when the Controller starts to process the request, Spring will assign the Request object to the method parameter, and we can get the Request object by setting it directly to the parameter.</p> <p><a href="https://medium.com/@malvin.lok/three-ways-that-help-you-retrieve-request-object-from-spring-boot-d39d4faa64b1">Visit Now</a></p>