SEARCH BY

All

  • All
  • Java8
  • Spring

Using Custom Converters with @ConfigurationProperties in Spring Boot

Post a Comment

Introduction

In this tutorial iam trying to explain the way of creating custom converters while using @ConfigurationProperties. We know we can store string value in .properties file, but by default there is a conversion provided by spring boot to convert string to int ( and into another types ), for example let us consider property age is stored in .properties file as age=30, but here 30 is string in the view of .properties file. But while mapping age property in POJO class we can create age as integer variable. In this case age property will we converted to integer and mapped to age variable of POJO class.

But for example let us consider lastDate property in .property file is created as lastDate=12-01-2020 , then if we want to map this with Date object in a POJO class there is no inbuilt converter in spring boot. To handle this type of cases we need to create custom converters. Now in this tutorial we are going to dig into how to create custom converters while working with .properties mapping with POJO.

Spring converts external properties into targeted POJO class field type when we annotated a POJO class with @ConfigurationProperties with built in type conversion process only.

Using @ConfigurationPropertiesBinding to create custom converts

To create a custom converter we need to create a bean class which annotated with @ConfigurationPropertiesBinding and implements Converter<source_type, target_type> interface by overriding target_type convert(source_type) method.

application.proeprties

We are going to create one property named as lastDate in application.properties to convert string date type into Date object.
lastDate = 20-02-2020

POJO class for mapping with properties

@Configuration
@ConfigurationProperties
public class MessageConfig {

 LocalDate lastDate;

 public void setLocation1(String location1) {
  this.location1 = location1;
 }

 public LocalDate getLastDate() {
  return lastDate;
 }

 public void setLastDate(LocalDate lastDate) {
  this.lastDate = lastDate;
 }
 
}

Creating converter class

We are going to create a date converter class from String to date object conversion.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
@ConfigurationPropertiesBinding
public class String2DateConverter implements Converter<String, LocalDate> {

 @Override
 public LocalDate convert(String source) {
  // TODO Auto-generated method stub
  if (source == null) {
   return null;
  } else {
   return LocalDate.parse(source, DateTimeFormatter.ofPattern("MM-dd-yyyy"));
  }
 }

}
In the above code you can see that in line 9 we have annotated a class with @ConfigurationPropertiesBinding which will to converting, and in line 10 you can see that Converter has two generic types , the first parameter is String which is a source type and the second parameter is a LocalDate type which is target type. And finally in line 13 you can see convert() method with target type as a return type and source type as a input parameter.

Output

Here we have extracted year of the date to display in output for evidencing that string is converted into Date type.
Custom type conversion String to Date type
Custom type conversion String to Date type

References

jaya
I love software designing, coding, writing and teaching...

Share this article

Related Posts

Post a Comment

Place your code in <em> </em> tags to send in comments | Do not add hyper links in commnet

Close

Subscribe our newsletter for the latest articles directly into your email inbox