My application has nested properties stored in applicaion.yml file.
I want to map those properties to POJO at the application boot-up.
Application.yml:
demo:
- A:
- type: A
prop1: 1
prop2: 2
proop3: 3
- type: B
prop1: 1
prop2: 2
proop3: 3
- B:
- type: A
prop1: 1
prop2: 2
proop3: 3
- type: B
prop1: 1
prop2: 2
proop3: 3
In order to achieve that, I am using below annotations:
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties("demo")
Class Demo:
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties("demo")
public class Demo {
@JsonProperty("A")
private List<A> a = null;
@JsonProperty("B")
private List<B> b = null;
@JsonProperty("A")
public List<A> getA() {
return a;
}
@JsonProperty("A")
public void setA(List<A> a) {
this.a = a;
}
@JsonProperty("B")
public List<B> getB() {
return b;
}
@JsonProperty("B")
public void setB(List<B> b) {
this.b = b;
}
@Override
public String toString() {
return "Demo [a=" + a + ", b=" + b + "]";
}
}
Class A:
public class A {
@JsonProperty("type")
private String type;
@JsonProperty("prop1")
private Integer prop1;
@JsonProperty("prop2")
private Integer prop2;
@JsonProperty("proop3")
private Integer proop3;
@JsonProperty("type")
public String getType() {
return type;
}
@JsonProperty("type")
public void setType(String type) {
this.type = type;
}
@JsonProperty("prop1")
public Integer getProp1() {
return prop1;
}
@JsonProperty("prop1")
public void setProp1(Integer prop1) {
this.prop1 = prop1;
}
@JsonProperty("prop2")
public Integer getProp2() {
return prop2;
}
@JsonProperty("prop2")
public void setProp2(Integer prop2) {
this.prop2 = prop2;
}
@JsonProperty("proop3")
public Integer getProop3() {
return proop3;
}
@JsonProperty("proop3")
public void setProop3(Integer proop3) {
this.proop3 = proop3;
}
@Override
public String toString() {
return "A [type=" + type + ", prop1=" + prop1 + ", prop2=" + prop2 + ", proop3=" + proop3 + "]";
}
}
Class B
public class B {
@JsonProperty("type")
private String type;
@JsonProperty("prop1")
private Integer prop1;
@JsonProperty("prop2")
private Integer prop2;
@JsonProperty("proop3")
private Integer proop3;
@JsonProperty("type")
public String getType() {
return type;
}
@JsonProperty("type")
public void setType(String type) {
this.type = type;
}
@JsonProperty("prop1")
public Integer getProp1() {
return prop1;
}
@JsonProperty("prop1")
public void setProp1(Integer prop1) {
this.prop1 = prop1;
}
@JsonProperty("prop2")
public Integer getProp2() {
return prop2;
}
@JsonProperty("prop2")
public void setProp2(Integer prop2) {
this.prop2 = prop2;
}
@JsonProperty("proop3")
public Integer getProop3() {
return proop3;
}
@JsonProperty("proop3")
public void setProop3(Integer proop3) {
this.proop3 = proop3;
}
@Override
public String toString() {
return "B [type=" + type + ", prop1=" + prop1 + ", prop2=" + prop2 + ", proop3=" + proop3 + "]";
}
}
Main Class
@SpringBootApplication
@ComponentScan(basePackages = {"com.microservice.*"})
@EnableJpaRepositories("com.microservice.*")
@EntityScan("com.microservice.*")
public class MainApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MainApplication.class);
app.run();
System.out.println("step 1");
Demo config = new Demo();
System.out.println("name: " + config);
}
public void run(String... args) throws Exception {
System.out.println("step 2");
}
}
But I am getting below o/p:
step 1
name: Demo [a=null, b=null]