Spring Boot YAML 绑定:绑定属性失败
Posted
技术标签:
【中文标题】Spring Boot YAML 绑定:绑定属性失败【英文标题】:Spring Boot YAML binding: Failed to bind properties 【发布时间】:2019-05-12 16:35:27 【问题描述】:我正在尝试使用 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
有人知道发生了什么吗?任何帮助将不胜感激。
【问题讨论】:
你的 YAML 文件缩进是否正确?如果name
和 expectedState
没有缩进为 entry
的子级,您将遇到遇到的错误。
您是否尝试过将预期状态的类型从 NodeState 更改为字符串?因为根据属性配置,它看起来像字符串。
这个枚举 com.project.mmgenerator.input.Entry 的值是多少?
【参考方案1】:
尝试去掉'name:'的值中的双引号("),Spring会自动将其绑定为String,你不需要将其指定为"string"
【讨论】:
以上是关于Spring Boot YAML 绑定:绑定属性失败的主要内容,如果未能解决你的问题,请参考以下文章