Spring Boot Get Base URL

Tags: ServletUriComponentsBuilder Base URL

In this Spring Boot tutorial we learn how to get base URL in a Spring Boot Web or Restful API application.

Table of contents

  1. Create New Spring Boot Web Project
  2. Implement Utility Class to Get Application Base URL
  3. Add New RestController to Return Base URL
  4. Complete Source Code and Run Application
  5. Download The Complete Source Code

Create New Spring Boot Web Project

Open IntelliJ IDEA, select the menu File > New > Project. (or click on New Project button at IntelliJ Welcome dialog)

On the New Project dialog, select Spring Initializr and click Next button.

Spring Boot Get Base URL

On the Spring Initializr Project Settings dialog input the new project information as below and click Next button.

  • Group: dev.simplesolution
  • Artifact: spring-boot-get-base-url
  • Type: Gradle
  • Language: Java
  • Version: 1.0.0
  • Name: spring-boot-get-base-url
  • Description: Spring Boot Get Base URL
  • Package: dev.simplesolution.springbootbaseurl

Spring Boot Get Base URL

On the Dependencies dialog, select below dependencies and click Next button.

  • Spring Web

Spring Boot Get Base URL

Select the location for your project and click Finish button to create new Spring Boot project.

Spring Boot Get Base URL

Implement Utility Class to Get Application Base URL

At this first step we implement a new Java utility class to get base URL value from a given HttpServletRequest object using the Spring Web MVC API.

Add a new Java package named dev.simplesolution.springbootbaseurl.util to your project and create a new Java class named UrlUtil as the following Java code.

src/main/java/dev/simplesolution/springbootbaseurl/util/UrlUtil.javaUrlUtil.java

package dev.simplesolution.springbootbaseurl.util;

import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.servlet.http.HttpServletRequest;

public class UrlUtil {

    /**
     * This method to get base URL of the application
     * @param request the HttpServletRequest object
     * @return the base URL
     */
    public static String getBaseUrl(HttpServletRequest request) {
        String baseUrl = ServletUriComponentsBuilder
                .fromRequestUri(request)
                .replacePath(null)
                .build()
                .toUriString();

        return baseUrl;
    }
}

Add New RestController to Return Base URL

At this step we implement a sample rest controller class to return the base URL value.

Add a new Java package named dev.simplesolution.springbootbaseurl.controller and implement a new RestController class named URLController as Java code below.

src/main/java/dev/simplesolution/springbootbaseurl/controller/URLController.java

package dev.simplesolution.springbootbaseurl.controller;

import dev.simplesolution.springbootbaseurl.util.UrlUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class URLController {

    @GetMapping("/getBaseUrl")
    public String getBaseUrl(HttpServletRequest request) {
        String baseUrl = UrlUtil.getBaseUrl(request);
        return baseUrl;
    }
}

Complete Source Code and Run Application

At this step we have finished implementation for a Spring Boot application which has an Restful API to return base URL with source code structure as screenshot below.

Spring Boot Get Base URL

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.

Spring Boot Get Base URL

Download The Complete Source Code

The source code in this article can be found at: github.com/simplesolutiondev/spring-boot-get-base-url

or clone at:

git clone https://github.com/simplesolutiondev/spring-boot-get-base-url.git

or download at:

Download Source Code

Happy Coding 😊

Spring Boot Web with JSP

Spring Boot Console Application using CommandLineRunner

Spring Boot Web Get Client IP Address