Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the base URL of the current webapp in Spring?

I'd like to find the absolute URL of the webapp in Spring, from the Controller. I'm aware of JSTL c:url, but I need this info from inside the Controller.

@Controller
public class AuthorizeController {

    @Autowired
    private Authorizer auth;

    @RequestMapping("/auth")
    public String sendToAuthorization() {        
        String baseUrl = "http://localhost:8080/tasks/";
        return "redirect:" +  auth.getAuthorizationUrl(baseUrl);
    }

}

As you can see the baseUrl is hardcoded, and I could provide it to the Authorizer class via Spring configuration, but I am sure that it's possible to get this information from Spring within the Controller. I tried to google "spring mvc url" and could not find a way to solve this problem.

like image 912
stivlo Avatar asked Jul 25 '11 10:07

stivlo


People also ask

How do I find the URL of a base URL?

There's nothing in the Android URI class that gives you the base URL directly- As gnuf suggests, you'd have to construct it as the protocol + getHost(). The string parsing way might be easier and let you avoid stuffing everything in a try/catch block.

How do I find the URL of a spring project?

Run the Spring Boot Web application and open the URL http://localhost:8080/getBaseUrl on your browser to receive the base URL result as the following screenshot.

What is my spring boot application URL?

By default, Spring boot applications are accessed by context path “/” which is default for embedded servers i.e. we can access the application directly at http://localhost:PORT/ .

What is base URL in app?

What Does Base URL Mean? In web development, design applications can define a base URL or base location, which helps in converting relative web URLs on the specific page to absolute web URLs. The HTML element <base> allows a base URL for use for all relative URLs within a specific document.

How to retrieve the context path in a spring web application?

In this tutorial, we discuss 2 ways for retrieving the context path in a Spring Web application. The typical way of getting the context path is through the HttpServletRequest class. Simply you can add a HttpServletRequest parameter to your controller method and then get the context path using getContextPath () method.

How do I get the base URL of a WebRequest?

If you want to get the base URL from a WebRequest you can do the following: ServletUriComponentsBuilder.fromRequestUri (HttpServletRequest request); This will give you the scheme ("http" or "https"), host ("example.com"), port ("8080") and the path ("/some/path"), while fromRequest (request) would give you the query parameters as well.

How to-get current URL with JavaScript?

How TO - Get Current URL With JavaScript ❮ PreviousNext ❯ Learn how to get the current URL with JavaScript. Current URL Use window.location.hrefto get the current URL address: Example document.getElementById("demo").innerHTML = "The full URL of this page is:<br>" + window.location.href; Try it Yourself »

Do I need the absolute URL or base URL?

For example the correct URL is: Do you need the absolute URL? Or you just want to build a internal link to another page in your application? I need the absolute URL. This works perfectly. It returns the current action URL without the base URL and query strings. This does not work.


2 Answers

I think that getting absolute url is only possible while processing the request as your server may have many IP addresses and domain names.

 @RequestMapping("/auth")
    public String sendToAuthorization(HttpServletRequest request) {        
    String baseUrl = String.format("%s://%s:%d/tasks/",request.getScheme(),  request.getServerName(), request.getServerPort());


        return "redirect:" +  auth.getAuthorizationUrl(baseUrl);
    }

As for the servlet, it may also have several mappings in web.xml.

similar question

P.S. Anyway, url parsing in runtime does not look like a good idea to me.

like image 144
Boris Treukhov Avatar answered Oct 21 '22 23:10

Boris Treukhov


Very late to this answer, but a variant to Boris's answer, if you don't want to push servlet objects into method signatures, is to use RequestContextHolder from a utility class/method. This would also give the ability to abstract fallback logic (e.g., pulling from a property file). Cheesy example:

RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if(null != requestAttributes && requestAttributes instanceof ServletRequestAttributes) {
  HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
  // build URL from request
}
else {
  // fallback logic if request won't work...
}

This presumes you have org.springframework.web.context.request.RequestContextListener registered as a listener in web.xml

like image 11
bimsapi Avatar answered Oct 21 '22 23:10

bimsapi