Announcement Announcement Module
Collapse
No announcement yet.
getting ip address with annotation controllers while staying platform agnostic Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • getting ip address with annotation controllers while staying platform agnostic

    hi there,

    we are using the amazing annotation based controller setup, which works like a treat - excellent work all round

    is there any way of retrieving the ip address of the request without resorting to HttpServletRequest objects (either directly as a parameter, or via NativeWebRequest) - this seems to tie us to a given servlet implementation, and we'd prefer to be completely platform-agnostic if possible

    thanks for any tips,
    chris

  • #2
    HttpServletRequest does not tie you to an implementation

    Hi Chris,

    HttpServletRequest is part of the standard Java servlet API, so using it does not tie you to an implementation. However, if you would like to get the client IP address without having to add HttpServletRequest as one of your method parameters you could use the following approach to isolate the HttpServletRequest to a single method on a controller:

    Code:
    @Controller
    @RequestMapping("/some.action")
    public final class SomeController {
    
    	@ModelAttribute("clientIpAddress")
    	public String populateClientIpAddress(
    			HttpServletRequest request) {
    
    		return request.getRemoteAddr();
    	}
    
    	@RequestMapping(method = RequestMethod.GET)
    	public String handleRequest(
    			@ModelAttribute("clientIpAddress") String clientIpAddress,
    			ModelMap model) throws Exception { 
    
    		// handle request without referencing servlet API
    
    		return "view";
    	}
    }

    Comment


    • #3
      ahhh, thanks for clearing that up. i thought that our reliance on catalina.jar (we use tomcat normally) was a problem, but we simply switched in the jboss jar instead and it worked just fine

      and thanks for the clever solution, @ModelAttribute absolutely rocks my world

      Comment

      Working...
      X