Spring Boot - Easy customizations for your applications!
Tags: java, kotlin, springboot, spring
Published: 2022-01-29 12:55:55

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m82kwlexag9yo9crbg26.png)

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](https://gist.github.com/tremaineeto/b3918287ed3128ac1d4f1a2f1ef6c39d). Other formatting includes [AnsiColor](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/ansi/AnsiColor.html) and [AnsiStyle](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/ansi/AnsiStyle.html) (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](https://patorjk.com/software/taag/)) 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}
  ${AnsiColor.DEFAULT}

```

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

![Spring Boot custom banner](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ma8c8kcz587eivu9u4nf.png)

  If you are the more boring type, you can also remove the banner with a yaml property:
```yaml
    spring:
      main:
        show_banner: false
```



## 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:
```html
  <html>
      <head>
          <title>My awesome little website!</title>
      </head>
      <body>
          <h1>Welcome to this awesome server!</h1>
          <br />
          <a href="#">Documentation</a>
      </body>
  </html>
```

Then we can open the browser, and voila!

![Spring Boot hosting static site](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/itz50wh617csmqeyaplf.png)

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](https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html) 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](https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html) (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](https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints.implementing-custom) and [custom FailureAnalyzer](https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.application.failure-analyzer) (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!