3

In my spring webservice I'm trying to get the url of my application like "http://localhost:8080/mycontext". My service does not contain any HttpServletRequest var, so I can I get it ?

2
  • 1
    You might need to add HttpServletRequest as a parameter. stackoverflow.com/questions/1490821/… Commented Jun 21, 2017 at 12:40
  • 1
    HttpServletRequest can be injected by Spring in your controller's methods, just add it in the parameters. Commented Jun 21, 2017 at 12:47

1 Answer 1

8

You can get the current request via the RequestContextHolder like:

ServletRequestAttributes sra = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest req = sra.getRequest();     
req.getContextPath();
req.getPathInfo();

Or you can just inject it via Spring into your service:

private @Autowired HttpServletRequest request;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ! I did it with private @Autowired HttpServletRequest request;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.