Java, Software Development, Spring, Spring Boot

Trim String Values From Properties Files in Spring Boot

This post is about how to trim string values from Properties files using SpringApplicationBuilder and StandardEnvironment.

Requirements

This post uses the following.

  • Java 8 (JDK)
  • IntelliJ IDEA Ultimate 2016.3
  • Spring Boot 1.5.6.RELEASE

Trim String Values Use-case

Our codes must trim all leading and trailing spaces from parameter values. We have a Properties file as shown in the image below.

The parameter key has a string value with trailing spaces. If we use it as is, we will have the following output.

Read Properties Files Spring Boot

The solution is to trim the String values in the getProperty methods of StandardEnvironment as shown in the code snippet below. First, we create an object of a subclass or anonymous class. Second, we override the 2 getProperty methods to returned trimmed String values. Third, we create an instance of SpringApplicationBuilder and set its environment.

This outputs:

Download the codes

The codes for this post are available at GitHub.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like