alibaba / nacos

an easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications.
https://nacos.io
Apache License 2.0
30.33k stars 12.85k forks source link

[Configuration Service]How to convert map structure from YAML style dataId to entity by using @NacosConfigurationProperties #6319

Closed Beemo37 closed 2 years ago

Beemo37 commented 3 years ago

Issue Description

I'm doing an experiment and trying to convert map structure from configuration server to local entity, but I failed. I know we have a nacos-spring-config-pojo-example demo, but it only contains simple type but doesn't mention the map type. And I also search the issues but didn't find a duplicated question. Do I write the code by mistake? Type: bug report

Describe what happened (or what feature you want)

Can't convert map structure from a YAML style dataId to entity, and when I debug, the key of PropertyValue seems to be wrong. image

Describe what you expected to happen

Can conver map strucure to entity.

How to reproduce it (as minimally and precisely as possible)

First create a dataId with name "example" and type yaml, and then declare configuration as follows:

user:
  info:
    name: beemo
    age: 18
    phoneNumbers: 1111111, 2222222, 333333
    classes: 
      grade1: 3
      grade2: 8

And I only have three java files, which are:

class Main {
  public static void main(String[] args) throws InterruptedException {
    ApplicationContext ctx = new AnnotationConfigApplicationContext("songyang.demo.nacos.spring.pojo");
    User bean = ctx.getBean(User.class);
    while (true) {
      System.out.println(bean);
      TimeUnit.SECONDS.sleep(5);
    }
  }
}
@Component
@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848"))
class NacosConfig {
  // Do Nothing
}
@Component
@NacosConfigurationProperties(dataId = "example", autoRefreshed = true, prefix = "user.info", type = ConfigType.YAML)
class User {

  private String name;
  private int age;
  private String[] phoneNumbers;
  private Map<String, String> classes;

  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 String[] getPhoneNumbers() {
    return phoneNumbers;
  }

  public void setPhoneNumbers(String[] phoneNumbers) {
    this.phoneNumbers = phoneNumbers;
  }

  public Map<String, String> getClasses() {
    return classes;
  }

  public void setClasses(Map<String, String> classes) {
    this.classes = classes;
  }

  @Override
  public String toString() {
    return "User{" +
        "name='" + name + '\'' +
        ", age=" + age +
        ", phoneNumbers=" + Arrays.toString(phoneNumbers) +
        ", classes=" + classes +
        '}';
  }
}

Tell us your environment

mase86 commented 3 years ago

Fix all

stale[bot] commented 2 years ago

Thanks for your feedback and contribution. But the issue/pull request has not had recent activity more than 180 days. This issue/pull request will be closed if no further activity occurs 7 days later. We may solve this issue in new version. So can you upgrade to newest version and retry? If there are still issues or want to contribute again. Please create new issue or pull request again.