DEV Community

Marie K. Ekeberg
Marie K. Ekeberg

Posted on • Originally published at

Spring Boot - Easy customizations for your applications!

In this article I will show you two types of easy customizations you can do with your Spring Boot applications. One of them can be very useful, and one is mostly cosmetic for the benefit of those seeing the server logs. This article is language agnostic, so it can be used for all programming languages you might make Spring Boot applications in (probably Java or Kotlin).

Custom banner

When you start your Spring Boot application you will see a banner like this:

Spring Boot basic banner

This is pretty standard, and maybe a bit boring. Did you know that you can create your own? Just create a banner.txt file in your src/main/resources directory, and it will show at startup! Those of you who want to do more advanced colors, formatting, showing versions etc. might wonder how that is done? There are several variables for that, some are documented in a great way in this Github Gist I found. Other formatting includes AnsiColor and AnsiStyle (links are for the enums found in the current version of Spring Boot). Using a collection of these and some ascii art (generated by this website) we can create a cool banner for our very cool server:

  ${AnsiColor.GREEN} ________  ________  ________  ___               ________  _______   ________  ___      ___ _______   ________     
  |\   ____\|\   __  \|\   __  \|\  \             |\   ____\|\  ___ \ |\   __  \|\  \    /  /|\  ___ \ |\   __  \    
  \ \  \___|\ \  \|\  \ \  \|\  \ \  \            \ \  \___|\ \   __/|\ \  \|\  \ \  \  /  / | \   __/|\ \  \|\  \   
   \ \  \    \ \  \\\  \ \  \\\  \ \  \            \ \_____  \ \  \_|/_\ \   _  _\ \  \/  / / \ \  \_|/_\ \   _  _\  
    \ \  \____\ \  \\\  \ \  \\\  \ \  \____        \|____|\  \ \  \_|\ \ \  \\  \\ \    / /   \ \  \_|\ \ \  \\  \| 
     \ \_______\ \_______\ \_______\ \_______\        ____\_\  \ \_______\ \__\\ _\\ \__/ /     \ \_______\ \__\\ _\ 
      \|_______|\|_______|\|_______|\|_______|       |\_________\|_______|\|__|\|__|\|__|/       \|_______|\|__|\|__|

                                        ${AnsiStyle.BOLD}${AnsiColor.RED}- Spring Boot Version ${spring-boot.formatted-version} -${AnsiStyle.NORMAL}

Enter fullscreen mode Exit fullscreen mode

Here we see some of the codes in action! So how might this look? Like this:

Spring Boot custom banner

If you are the more boring type, you can also remove the banner with a yaml property:

        show_banner: false
Enter fullscreen mode Exit fullscreen mode

Static web pages

Now time for the most known feature in this article: hosting static webpages with Spring Boot. You may or may not know that files found in src/main/resources/static are hosted as webpages when running your application? This means you can make an index.html file that is served when accessing the root path of your server in the browser. Try making one, and run mvn spring-boot:run (if you use Maven), and you can access it at localhost:8080 (unless a different URL is configured). To show you an example, here is an index.html file:

          <title>My awesome little website!</title>
          <h1>Welcome to this awesome server!</h1>
          <br />
          <a href="#">Documentation</a>
Enter fullscreen mode Exit fullscreen mode

Then we can open the browser, and voila!

Spring Boot hosting static site

What are these websites useful for? Other than being fun to have, off course. Some ideas for information you might want to show:

  • Maybe you have some documentation that the user of your server might want to read? Swagger pages, SOAP WSDL files, API documentation for possible integration code, link to relevant web pages to get people up to speed with relevant concepts etc.
  • URLs of other APIs your servers communicate with, unless they should be masked (some more proprietary solutions still do this). Most applications don't like in a vacuum anymore, and you might use different URLs in test vs production (which you will see fast if shown on a site like this).
  • Build information like versions, user who last committed changes etc.
  • Spring Boot Actuator link (these are sometimes configured to be other places than the default)
  • Spring Boot Actuator calls to provide information directly in the site (might require some JavaScript). Monitoring etc.

So they are not just for the developers having fun, they can provide real value as well!

You can also provide your own error web pages, with the most famous example being 404 errors. This is as easy as what we did above, with one simple tweak: The sites need to be in the error-subdirectory (inside your src/main/resources/static directory). So your 404 page would be at this path src/main/resources/static/error/404.html.

If you want to make more advanced web pages, this can be done with something like Thymeleaf (templating system), JSP, or just with some JavaScript code. You would probably want to have something like stylesheets and other files included to make your sites more fancy as well. The site above was kept simple for the sake of example (and for you to have something to copy-paste to test quickly if you want to!).

I focused on more general tips that applies to most Spring Applications in this article. There are off course more customizations (that are more dependent on your problem domain) that can be done, including custom Actuator endpoints and custom FailureAnalyzer (when application fails to start), but these are topics for a separate article.

Do you have any other fun customizations for Spring Boot? Feel free to mention them in the comments!

Top comments (0)