Spring Boot YAML绑定:无法绑定属性

时间:2018-12-11 16:23:28

标签: spring-boot data-binding yaml

我正在尝试使用 Spring Boot 2.0.5.RELEASE

generator.yaml文件与类型为GeneratorConfig的对象绑定

generator.yaml

entry:
  name: "node-1"
  expectedState: HEALTHY

@Component
@PropertySource("classpath:generator.yaml")
@ConfigurationProperties
public class GeneratorConfig {
    private Entry entry;

    // getter and setter
}

public class Entry {

    private String name;
    private NodeState expectedState;

    // getters and setters
}

我收到以下异常:

2018-12-11 13:11:53.885  INFO 3923 --- [           main] com.project.mmgenerator.MMGenerator     : Starting MMGenerator on madmin’s-MacBook-Pro with PID 3923 (/Users/jscherman/IdeaProjects/sentinel/mock-metrics-generator/out/production/classes started by jscherman in /Users/jscherman/IdeaProjects/sentinel)
2018-12-11 13:11:53.889 DEBUG 3923 --- [           main] com.project.mmgenerator.MMGenerator     : Running with Spring Boot v2.0.5.RELEASE, Spring v5.0.9.RELEASE
2018-12-11 13:11:53.890  INFO 3923 --- [           main] com.project.mmgenerator.MMGenerator     : No active profile set, falling back to default profiles: default
2018-12-11 13:11:53.934  INFO 3923 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3eb91815: startup date [Tue Dec 11 13:11:53 ART 2018]; root of context hierarchy
2018-12-11 13:11:55.292  INFO 3923 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2018-12-11 13:11:55.314  INFO 3923 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-12-11 13:11:55.314  INFO 3923 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.34
2018-12-11 13:11:55.318  INFO 3923 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/jscherman/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2018-12-11 13:11:55.403  INFO 3923 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-12-11 13:11:55.403  INFO 3923 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1471 ms
2018-12-11 13:11:56.209  INFO 3923 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-12-11 13:11:56.210  INFO 3923 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'webMvcMetricsFilter' to: [/*]
2018-12-11 13:11:56.210  INFO 3923 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-12-11 13:11:56.210  INFO 3923 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-12-11 13:11:56.210  INFO 3923 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-12-11 13:11:56.210  INFO 3923 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpTraceFilter' to: [/*]
2018-12-11 13:11:56.210  INFO 3923 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-12-11 13:11:56.317  INFO 3923 --- [           main] c.m.s.metric.InfluxDBMetricsConnector    : Setting InfluxDB connector with database name sentinel_db
2018-12-11 13:11:56.345  INFO 3923 --- [           main] c.m.mmgenerator.adapter.AdapterService   : Building adapter service (sentinel-url=http://localhost:8080/, mmgenerator-url=http://localhost:8081/)
2018-12-11 13:11:56.431  WARN 3923 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'generatorEntriesController' defined in file [/Users/jscherman/IdeaProjects/sentinel/mock-metrics-generator/out/production/classes/com/project/mmgenerator/entry/GeneratorEntriesController.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'generatorConfig': Could not bind properties to 'GeneratorConfig' : prefix=, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'entry' to com.project.mmgenerator.input.Entry
2018-12-11 13:11:56.433  INFO 3923 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-12-11 13:11:56.447  INFO 3923 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-12-11 13:11:56.451 ERROR 3923 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'entry' to com.project.mmgenerator.input.Entry:

    Property: entry
    Value: 
    Origin: "entry" from property source "class path resource [generator.yaml]"
    Reason: No converter found capable of converting from type [java.lang.String] to type [com.project.mmgenerator.input.Entry]

Action:

Update your application's configuration

有人知道发生了什么吗?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试从'name:'的值中删除双引号(“),Spring会自动将其绑定为String,而无需将其指定为” string“