SoFunction
Updated on 2025-03-08

How to get request in springMVC controller

This article mainly introduces how to obtain requests in springMVC controller. The example code is introduced in this article in detail, which has certain reference value for everyone's study or work. Friends who need it can refer to it.

ServletRequestAttributes attributes = (ServletRequestAttributes) ();
HttpServletRequest request = ();
HttpServletResponse response = ();
try {
  ().write("hello");
} catch (IOException e) {
  ();
}
Enumeration<String> headerNames = ();
while (()) {
  String name = ();
  String value = (name);
  (name + "===========" + value);
}

When using springMVC, sometimes you need to get the request or response object. For example, when authenticating, you need to get the token in the request header. When logging in to the system, you need to use the response object to add cookies to the client. An effective method is to add corresponding parameters to the controller method as follows:

@RestController
public class Test2Contrller {
  @RequestMapping("/test")
  public void test(HttpServletRequest req, HttpServletResponse res) {
// todo
 }
}

There is a problem with this, that is, if the system is used as an interface and wants to be called remotely, the existence of additional parameters will destroy the original interface definition and cause trouble. The following are two ways to obtain request and response without adding additional parameters to the method

The first method: get requestAttributes through the method of the RequestContextHolder class, and then get the request and response objects from it;

@RestController
public class Test2Contrller {
  @RequestMapping("/testreq")
  public void test() {
    // Get the request object, the response object    ServletRequestAttributes attributes = (ServletRequestAttributes) ();
    HttpServletRequest request = ();
    HttpServletResponse response = ();
    try {
      ().write("hello");
    } catch (IOException e) {
      ();
    }
    Enumeration&lt;String&gt; headerNames = ();
    while (()) {
      String name = ();
      String value = (name);
      (name + "===========" + value);
    }
 
  }
}

The second method: you can extract the request and response objects and put them in a superclass. You need to use the controller of these two objects to inherit this class and use it directly. The code is as follows:

Super Class:

public class BaseController {
// Why are these objects directly used by subclasses  protected HttpServletRequest request;
  protected HttpServletResponse response;
  protected HttpSession session;
 
  @ModelAttribute
  public void setReqAndRes(HttpServletRequest req, HttpServletResponse res) {
     = req;
     = res;
     = ();
  }
}

Subclass:

@RestController
public class Test3Contrller extends BaseController{
  @RequestMapping("/testreq2")
  public void test() {
    try {
      ().write("hello");
    } catch (IOException e) {
      ();
    }
    Enumeration<String> headerNames = ();
    while (()) {
      String name = ();
      String value = (name);
      (name + "===========" + value);
    }
 
  }
}

You can see that the code of the second method is much simpler. If you have a lot of usage requirements for the request or response object, for example, you need to obtain user information from the token in the request header and dynamically return the result, it is recommended to use the second method;

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.