Book Image

Spring Boot Cookbook

By : Alex Antonov
Book Image

Spring Boot Cookbook

By: Alex Antonov

Overview of this book

Table of Contents (15 chapters)
Spring Boot Cookbook
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface
Index

Configuring custom static path mappings


In the previous recipe, we looked at how to tune the URL path mapping for requests and translate them into controller methods. It is also possible to control how our web application deals with static assets and the files that exist on the file system or are bundled in the deployable archive.

Let's say that we want to expose our internal application.properties file via the static web URL of http://localhost:8080/internal/application.properties from our application. To get started with this, proceed with the steps in the next section.

How to do it…

  1. Let's add a new method, addResourceHandlers, to the WebConfiguration class with the following content:

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
      registry.addResourceHandler("/internal/**")
        .addResourceLocations("classpath:/");
    }
  2. Start the application by running ./gradlew clean bootRun.

  3. Let's open http://localhost:8080/internal/application.properties in the browser to see...