Reputation: 165
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.
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.
Upvotes: 3
Views: 5296
Reputation: 1388
Seems like what you're trying to achieve can be done without conversion
root.products.productName1.name=FirstProduct
root.products.productName1.productID=1234
root.products.productName1.availableOptions=Opt1
root.products.productName1.processing=Parallel
root.products.productName2.name=SecondProduct
root.products.productName2.productID=5678
root.products.productName2.availableOptions=Opt2
root.products.productName2.processing=Parallel
Therefore productName1
, productName2
would be your map key names.
You can use it this way:
@ConfigurationProperties("root")
@Getter
@Setter
public class ProductProperties {
private Map<String, Product> products;
}
Upvotes: 2
Reputation: 472
You could do what @Daria has suggested, which is the simplier way to do it.
In case you're still wondering how to convert a list into a Map, it can be done using Streams:
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) {
return source.stream().collect(
Collectors.toMap(Product::getName,Function.identity));
}
}
It would generate a map with the product name as key value, and the product object as content value.
Upvotes: 1