Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
102 views
in Technique[技术] by (71.8m points)

java - Getting a binding property exception when loading the yml file into an object

I am trying to load a yml file with the following element into an object in a test class

yml file name is application-acl-config.yml and there are more yml files in the folder. I assume spring boot merges all of them into one when the application starts.

yml file in resources of main folder:

  logicalClientIdentifiers:
    -   applicationIdentifier:
            applicationId: x
            logicalClientIds:
            - x
    -   applicationIdentifier:
            applicationId: y
            logicalClientIds:
            - y

code in main folder for pojo class:

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "logicalClientIdentifiers")
public class LogicalClientIdentifiers{
        private class ApplicationIdentifier {
            private String applicationId;
            private List<String> logicalClientIds;

            public String getApplicationId() {
                return applicationId;
            }

            public void setApplicationId(String applicationId) {
                this.applicationId = applicationId;
            }

            public List<String> getLogicalClientIds() {
                return logicalClientIds;
            }

            public void setLogicalClientIds(List<String> logicalClientIds) {
                this.logicalClientIds = logicalClientIds;
            }
        }

        private List<ApplicationIdentifier> applicationIdentifiers;

        public List<ApplicationIdentifier> getApplicationIdentifiers() {
            return applicationIdentifiers;
        }

        public void setApplicationIdentifiers(List<ApplicationIdentifier> applicationIdentifiers) {
            this.applicationIdentifiers = applicationIdentifiers;
        }

    }

test code in test folder:

@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = LogicalClientIdentifiers.class, initializers = ConfigFileApplicationContextInitializer.class)
public class ChannelAuthorizationTest {

    @Autowired
    LogicalClientIdentifiers applicationAclConfigProps;


    @Test
    public void test(){
        System.out.println();
    }

}

I am getting this exception:

`Caused by: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'logicalClientIdentifiers': Could not bind properties to 'LogicalClientIdentifiers' : prefix=logicalClientIdentifiers, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.source.InvalidConfigurationPropertyNameException: Configuration property name 'logicalClientIdentifiers' is not` valid

I believe I am not creating the pojo properly to parse the yml file or the yml file is not being referenced properly.

question from:https://stackoverflow.com/questions/66050253/getting-a-binding-property-exception-when-loading-the-yml-file-into-an-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your Java class looks like you want to load this YAML:

logicalClientIdentifiers:
  applicationIdentifiers:
  - applicationId: x
    logicalClientIds:
    - x
  - applicationId: y
    logicalClientIds:
    - y

You need to be careful that the YAML structure and your class structure match. The YAML you show misses the applicationIdentifiers field in the class LogicalClientIdentifiers and instead contains a sequence of mappings where each contains only a key applicationIdentifier, which does not map to anything in your Java classes.

Your error message occurs because you specify an invalid prefix. As described in the docs,

The prefix value for the annotation must be in kebab case (lowercase and separated by -, such as acme.my-project.person).

so you have to use @ConfigurationProperties(prefix = "logical-client-identifiers").


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...