Mapping list in Yaml to list of objects in Spring Boot
The reason must be somewhere else. Using only Spring Boot 1.2.2 out of the box with no configuration, it Just Works. Have a look at this repo - can you get it to break?
https://github.com/konrad-garus/so-yaml
Are you sure the YAML file looks exactly the way you pasted? No extra whitespace, characters, special characters, mis-indentation or something of that sort? Is it possible you have another file elsewhere in the search path that is used instead of the one you're expecting?
Mapping YAML List to List of Objects in Spring Boot
As Stephave Nicoll mentioned, @Value
annotation has nothing to do with @ConfigurationProperties
. Just name fields in the inner POJO same as in configuration file and this should work:
@Configuration
@ConfigurationProperties(prefix="config")
@EnableConfigurationProperties
public class GatewayConfig
{
List<Gateway> gateways = new ArrayList<Gateway>();
// Getter/Setter for gateways
// ...
public static class Gateway
{
private String id;
private int nbrInputs;
private int nbrOutputs;
// Getters and Setters
// ...
}
}
Reaction on comment:
With plain Spring/Spring Boot, I don't think you can map fields with different names and load it to the list of gateways. There would be option to use plain @Value annotation, but your gateway count would need to be hard-coded:
@Component
public class Gateway0{
@Value("${config.gateways[0].id}")
private String id;
@Value("${config.gateways[0].nbrInputs}")
private int numInputs;
@Value("${config.gateways[0].nbrOutputs}")
private int numOutputs;
// Getters and Setters
// ...
}
Mapping list in yml to list of objects in Scala (Spring Boot)
I managed to solve this. Here is what I did:
- Changed
List
toArray
- Rewrite the class to have fields instead of constructor parameters
- Introduce
scala.beans.BeanProperty
Working code:
import scala.beans.BeanProperty
@Configuration
@ConfigurationProperties(prefix = "org-registry-list")
class OrgRegistryConfig() {
@BeanProperty
var organisations: Array[Organisation] = _
}
object OrgRegistryConfig {
class Organisation() {
@BeanProperty
var orgId: Long = _
@BeanProperty
var orgName: String = _
}
}
Mapping a list of objects from application.yml with custom root key in Spring Boot
From the ConfigurationProperties
prefix
is the prefix of the properties that are valid to bind to this object and in your case it means that tasks will be the prefix of all your configuration class properties including the tasks property that appears in your application.yml file. To avoid this situation you can use @ConfigurationProperties
instead of @ConfigurationProperties(prefix = "tasks")
.
Spring Boot inject List of Maps from application.yml
This was a "new" for me. I got this working by making cacheConfigs
one level deeper and the used the new top level name as the @ConfigurationProperties
param. Like this:
cache-configs-map:
cacheConfigs:
- cacheOne:
test: test1
- cacheTwo:
test: test2
- cacheThree:
test: test3
- cacheFor:
test: test4
Now, your configuration class looks like this:
@Configuration
public class Config{
@NoArgsConstructor @AllArgsConstructor( staticName = "of" )
@Getter @Setter
public static class C{
private List<Map<String, String>> cacheConfigs;
}
@Bean
@ConfigurationProperties(prefix = "cache-configs-map")
public C getC() {
return new C();
}
}
Spring Boot YAML configuration for a list of strings containing commas
It looks like this old version of Spring Boot splits arrays by commas and ignores any escaping attempt. I ended up with the following:
YAML (use semicolons instead of commas):
element:
list: >
aa;aa,
bb;bb
Java (replace ;
-> ,
in @PostConstruct
):
@Value("${element.list}")
protected String[] elementList;
@PostConstruct
private void postConstruct() {
for (int i = 0; i < elementList.length; i++) {
elementList[i] = elementList[i].replace(";", ",");
}
}
Related Topics
Creating an Animated 4X4 Grid in Java
Remove Last Character of a Stringbuilder
Create Whole Path Automatically When Writing to a New File
File Path Windows Format to Java Format
How to Improve the Performance of G.Drawimage() Method for Resizing Images
Matrix Multiplication in Apache Spark
How to Mock New Date() in Java Using Mockito
How to Analyze Information from a Java Core Dump
How to Add a Timeout Value When Using Java's Runtime.Exec()
Closing Bufferedreader and System.In
Download Attachments Using Java Mail
Jsoup Cookies for Https Scraping
Trouble with Gson Serializing an Arraylist of Pojo'S
How to Turn Off the Httpsession in Web.Xml