I am trying to create a Map<String, Product> from my .properties file in Spring boot 3.1. I have written my custom converter for this, but it seems that it is never executing. I haven't done custom conversions before and I am not sure if a converter is needed in this use case or if Spring boot is able to manage the conversion automagically (If I configure it correctly).
product-configurations.properties:
products.product[0].name=FirstProduct
products.product[0].productID=1234
products.product[0].availableOptions=Opt1
products.product[0].processing=Parallel
Product class:
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Product {
private String name;
private String processing;
private String availableOptions;
private Integer productID;
}
Configuration class:
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import lombok.Getter;
import lombok.Setter;
@Configuration
@PropertySource("classpath:product-configurations.properties")
@ConfigurationProperties("products")
@Getter
@Setter
public class ProductProperties {
private List<Product> product;
}
ProductConverter class:
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import com.cb.clientfacingapp.model.Product;
@Component
@ConfigurationPropertiesBinding
public class ProductConverter implements Converter<List<Product>, Map<String, Product>> {
@Override
public Map<String, Product> convert(List<Product> source) {
Product p = new Product();
System.out.println("I am converting");
return null;
}
}
Can someone pls guide me on this?
Thanks in Advance.
Update:
Sorry, I forgot to mention how the map should be organized.
Map<String, Product> where String is basically the name property of the product, and Product is the POJO class for that specific product, So each Product is mapped to its Name, basically.