Is there any way in Spring 3 MVC to gain access to the request header information (like source IP address etc.), when parsing a request in a @Controller?
You can retrieve it from HttpServletRequest
, using getRemoteAddr()
to get access to user IP address and getHeader()
to get header value.
For example
@Controller
public class MyController {
@RequestMapping(value="/do-something")
public void doSomething(HttpServletRequest request) {
final String userIpAddress = request.getRemoteAddr();
final String userAgent = request.getHeader("user-agent");
....
}
}
You may pass other parameters to the doSomething()
method, like model or request params.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With