spring cloud config server NullPointerException

MamyA 2018-09-03 11:48:14
我在本地使用本地目录作为spring cloud config server的配置中心存储(JDK1.8),启动报空指针,找不到searchLocations的配置,配置如下:
----
server:
port: 8080
eureka:
instance:
hostname: 192.168.1.1
non-secure-port: ${server.port}
spring:
application:
name: config-server
config:
name: config-file
profiles:
active:
- native
cloud:
config:
server:
native:
version: 0.0.1
order: 1
search-locations:
- file:///E:/config
----


POM.xml配置如下:
-----
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>compile</scope>
</dependency>
<!--eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
-----



启动报错如下:
-----
2018-09-03 11:43:17.010 |-INFO [localhost-startStop-1] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping [362] -| Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-03 11:43:17.010 |-INFO [localhost-startStop-1] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping [362] -| Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-03 11:43:17.057 |-INFO [localhost-startStop-1] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping [362] -| Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-03 11:43:17.073 |-INFO [localhost-startStop-1] org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WelcomePageHandlerMapping [549] -| Adding welcome page: ServletContext resource [/index.html]
2018-09-03 11:43:17.400 |-ERROR [localhost-startStop-1] org.springframework.boot.bind.PropertiesConfigurationFactory [350] -| Properties configuration failed validation
2018-09-03 11:43:17.400 |-ERROR [localhost-startStop-1] org.springframework.boot.bind.PropertiesConfigurationFactory [352] -| Field error in object 'spring.cloud.config.server.native' on field 'searchLocations': rejected value [null]; codes [methodInvocation.spring.cloud.config.server.native.searchLocations,methodInvocation.searchLocations,methodInvocation.[Ljava.lang.String;,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.cloud.config.server.native.searchLocations,searchLocations]; arguments []; default message [searchLocations]]; default message [Property 'searchLocations' threw exception; nested exception is java.lang.NullPointerException]
2018-09-03 11:43:17.416 |-ERROR [localhost-startStop-1] org.springframework.boot.bind.PropertiesConfigurationFactory [352] -| Field error in object 'spring.cloud.config.server.native' on field 'searchLocations': rejected value [null,null]; codes [methodInvocation.spring.cloud.config.server.native.searchLocations,methodInvocation.searchLocations,methodInvocation.[Ljava.lang.String;,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.cloud.config.server.native.searchLocations,searchLocations]; arguments []; default message [searchLocations]]; default message [Property 'searchLocations' threw exception; nested exception is java.lang.NullPointerException]
2018-09-03 11:43:17.416 |-WARN [localhost-startStop-1] org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext [551] -| Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration$$EnhancerBySpringCGLIB$$1becfed2]: Constructor threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'configServerHealthIndicator' defined in class path resource [org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.class]: Unsatisfied dependency expressed through method 'configServerHealthIndicator' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.config.server.config.CompositeConfiguration': Unsatisfied dependency expressed through method 'setEnvironmentRepos' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'nativeEnvironmentRepository': Could not bind properties to NativeEnvironmentRepository (prefix=spring.cloud.config.server.native, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 2 errors
Field error in object 'spring.cloud.config.server.native' on field 'searchLocations': rejected value [null]; codes [methodInvocation.spring.cloud.config.server.native.searchLocations,methodInvocation.searchLocations,methodInvocation.[Ljava.lang.String;,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.cloud.config.server.native.searchLocations,searchLocations]; arguments []; default message [searchLocations]]; default message [Property 'searchLocations' threw exception; nested exception is java.lang.NullPointerException]
Field error in object 'spring.cloud.config.server.native' on field 'searchLocations': rejected value [null,null]; codes [methodInvocation.spring.cloud.config.server.native.searchLocations,methodInvocation.searchLocations,methodInvocation.[Ljava.lang.String;,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.cloud.config.server.native.searchLocations,searchLocations]; arguments []; default message [searchLocations]]; default message [Property 'searchLocations' threw exception; nested exception is java.lang.NullPointerException]
2018-09-03 11:43:17.416 |-INFO [localhost-startStop-1] org.springframework.jmx.export.annotation.AnnotationMBeanExporter [449] -| Unregistering JMX-exposed beans on shutdown
2018-09-03 11:43:17.416 |-INFO [localhost-startStop-1] org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer [101] -|

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-09-03 11:43:17.416 |-ERROR [localhost-startStop-1] org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter [42] -|

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

Description:

Binding to target org.springframework.cloud.config.server.environment.NativeEnvironmentRepository@5ab0b1ef failed:

Property: spring.cloud.config.server.native.searchLocations
Value: null
Reason: Property 'searchLocations' threw exception; nested exception is java.lang.NullPointerException

Property: spring.cloud.config.server.native.searchLocations
Value: null,null
Reason: Property 'searchLocations' threw exception; nested exception is java.lang.NullPointerException


Action:

Update your application's configuration



求大神帮忙推测可能的原因,我是按照官方文档进行搭建的,基本没有任何差异的地方,还是不行。求解。。。。。


...全文
234 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
MamyA 2018-09-03
  • 打赏
  • 举报
回复

终于解决这个问题了,贴出来让后续的玩家少踩坑。这个地方有一个误区,就是searchLocations这个值,查看官网配置和源码都是指明该值在native环境下为String []。问题就在这,spring cloud在反射填充NativeEnvironmentRepository的时候使用的是String而非String[],源码中是对searchLocations循环取值,所以没有做限定。导致我直接按照API在yml文件中写入一个String []无法注入进去,最终报空指针。修改为String值就OK了。其他信息如下:
spring cloud版本:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>

正确配置:
spring:
profiles:
active:
- native
cloud:
config:
server:
native:
search-locations: classpath:/config
MamyA 2018-09-03
  • 打赏
  • 举报
回复
刚刚把application.yml替换为application.properties文件,同样也是放在classpath根目录,竟然成功运行了,两个内容一样,如下:
application.yml:
spring:
profiles:
active:
- native
cloud:
config:
server:
native:
search-locations:
- classpath:/config


application.properties:
spring.profiles.active=native
spring.cloud.config.server.native.version=0.0.1
spring.cloud.config.server.native.order=0
spring.cloud.config.server.native.search-locations=classpath:/config


properties文件可以配置成功,而yml一直报没有设置searchLocations的错误,好奇怪啊。
MamyA 2018-09-03
  • 打赏
  • 举报
回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧