SoFunction
Updated on 2025-04-05

Summary of three ways to obtain Request requests in SpringBoot

The Request object contains various information about the request, such as request method, request URL, request parameters, request content, etc., which can be processed and responded to by the server. So in SpringBoot, how can I get the Request object?

This article will introduce three methods and provide example references.

1. Directly inject HttpServletRequest on the Controller method parameters

This is the most commonly used method. Inject the HttpServletRequest object directly on the Controller method parameters, and Spring will automatically assign the request object to this parameter.

Principle explanation: When Spring receives an HTTP request, it will find a suitable method to handle the request. If the method parameter is marked with @RequestMapping, @Get, @Post and other annotations, Spring will inject the HttpServletRequest object into the parameter.

Sample code:

@RestController
public class MyController {
    @RequestMapping("/test")
    public String test(HttpServletRequest request) {
        String ip = ();
        String method = ();
        String uri = ();
        return "ip:" + ip + ", method:" + method + ", uri:" + uri;
    }
}

2. Obtain through RequestContextHolder

In non-Controller methods, you can use RequestContextHolder to get the ServletRequestAttributes object, and then get the HttpServletRequest and HttpServletResponse from the object.

Principle explanation: Spring will encapsulate all request parameters, header information, etc. into the ServletRequestAttributes object. This object can be obtained by calling the getRequestAttributes() method of RequestContextHolder, and then the HttpServletRequest object can be obtained through the ServletRequestAttributes object.

Sample code:

@Service
public class MyService {
    public String test() {
        ServletRequestAttributes sra = (ServletRequestAttributes) ();
        HttpServletRequest request = ();
        String ip = ();
        String method = ();
        String uri = ();
        return "ip:" + ip + ", method:" + method + ", uri:" + uri;
    }
}

3. Inject the HttpServletRequest object through the @Autowired annotation

If you need to get the HttpServletRequest object in a non-Controller method, you can use the @Autowired annotation to inject the object into the corresponding variable.

Principle explanation: When initializing a bean, if you find that there is an attribute marked with @Autowired annotation in the bean, Spring will automatically find a suitable bean to inject into the property. If the property is an HttpServletRequest object, Spring injects the current request object into the property.

Sample code:

@Component
public class MyComponent {
    @Autowired
    private HttpServletRequest request;
    public String test() {
        String ip = ();
        String method = ();
        String uri = ();
        return "ip:" + ip + ", method:" + method + ", uri:" + uri;
    }
}

The above are three methods for SpringBoot to obtain Request, namely, directly injecting HttpServletRequest on the Controller method parameters, obtaining through RequestContextHolder, and injecting HttpServletRequest object through @Autowired annotation.

This is the end of this article about the three ways to obtain Request requests in SpringBoot. For more relevant contents for obtaining Request requests in SpringBoot, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!