I am trying to get the request URL in a RestController. The RestController has multiple methods annotated with @RequestMapping
for different URIs and I am wondering how I can get the absolute URL from the @RequestMapping
annotations.
@RestController @RequestMapping(value = "/my/absolute/url/{urlid}/tests" public class Test { @ResponseBody @RequestMapping(value "/",produces = "application/json") public String getURLValue(){ //get URL value here which should be in this case, for instance if urlid //is 1 in request then "/my/absolute/url/1/tests" String test = getURL ? return test; } }
We can use @RequestMapping with @RequestParam annotation to retrieve the URL parameter and map it to the method argument.
Spring RestController annotation is a convenience annotation that is itself annotated with @Controller and @ResponseBody . This annotation is applied to a class to mark it as a request handler. Spring RestController annotation is used to create RESTful web services using Spring MVC.
You may try adding an additional argument of type HttpServletRequest
to the getUrlValue()
method:
@RequestMapping(value ="/",produces = "application/json") public String getURLValue(HttpServletRequest request){ String test = request.getRequestURI(); return test; }
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