0

I want to read a request url using spring ,i have method like below and the client request url is like http://localhost:8080/api/getName ,i want to read (/api/getName from this url)

@Controller
public class TestController  {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseEntity<String> getDetails(
            final HttpServletRequest request,
            final HttpServletResponse response) throws Exception {

        }
Raghuram
  • 50,726
  • 11
  • 108
  • 122
user1195292
  • 223
  • 1
  • 3
  • 13

4 Answers4

1

It's a method in HttpServletRequest: request.getRequestURL() gives you the URL.

See this answer for some more details: How to get the request url from HttpServletRequest

To further analyse the URL, use it's methods: Parsing a URL

Community
  • 1
  • 1
Aaron Digulla
  • 310,263
  • 103
  • 579
  • 794
0

req.getContextPath();

Should get you the context path you are looking for.

ring bearer
  • 19,685
  • 7
  • 57
  • 70
0

You might also want to browse the api javadocs

http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html

bluesman
  • 2,184
  • 2
  • 24
  • 35
0

i want to read (/api/getName from this url)

For this you can use the HttpServletRequest method getRequestURI(). It will return the request URL, without the protocol/server/port part, and without the query String (the parameters).

Tomas Narros
  • 13,317
  • 2
  • 38
  • 56