How to extract IP Address in Spring MVC Controller get call?

后端 未结 9 562
长发绾君心
长发绾君心 2020-11-28 04:04

I am working on Spring MVC controller project in which I am making a GET URL call from the browser -

Below is the url by which I am making a GET call from the browse

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 04:45

    The solution is

    @RequestMapping(value = "processing", method = RequestMethod.GET)
    public @ResponseBody ProcessResponse processData(@RequestParam("workflow") final String workflow,
        @RequestParam("conf") final String value, @RequestParam("dc") final String dc, HttpServletRequest request) {
    
            System.out.println(workflow);
            System.out.println(value);
            System.out.println(dc);
            System.out.println(request.getRemoteAddr());
            // some other code
        }
    

    Add HttpServletRequest request to your method definition and then use the Servlet API

    Spring Documentation here said in

    15.3.2.3 Supported handler method arguments and return types

    Handler methods that are annotated with @RequestMapping can have very flexible signatures.
    Most of them can be used in arbitrary order (see below for more details).
    
    Request or response objects (Servlet API). Choose any specific request or response type,
    for example ServletRequest or HttpServletRequest
    

提交回复
热议问题