springboot2.x 复杂对象映射

model

@ConfigurationProperties(prefix = "person")
@PropertySource("classpath:application.yml")
public class Person {
    private String name;
    private int age;
    List friends;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public List getFriends() {
        return friends;
    }

    public void setFriends(List friends) {
        this.friends = friends;
    }
}
public class Friend {
    private int male;
    private String name;

    public int getMale() {
        return male;
    }

    public void setMale(int male) {
        this.male = male;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

controller

@RestController
@RequestMapping("/demo")
@Configuration
@EnableConfigurationProperties(Person.class)
public class DemoController {

    @Autowired
    Person person;

    @RequestMapping("/index")
    public Object index() {
        return person;
    }
}

配置

person:
  name: zhangsan
  age: 11
  friends:
    - male: 1
      name: 里斯
    - male: 2
      name: 王武

结果

springboot2.x 复杂对象映射_第1张图片

你可能感兴趣的:(springboot)