117

While creating Spring Boot cloud config application getting below error. Any help on this?

No spring.config.import property has been defined
 
Action:

Add a spring.config.import=configserver: property to your configuration.   
If configuration is not required add spring.config.import=optional:configserver: instead.
To disable this check, set spring.cloud.config.enabled=false or 
spring.cloud.config.import-check.enabled=false.
dur
  • 15,689
  • 25
  • 79
  • 125
Kamal
  • 1,173
  • 2
  • 5
  • 6

20 Answers20

198

Solution for Maven

Add the below dependency in the pom.xml file:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

Solution for Gradle

Add in the build.gradle file:

implementation('org.springframework.cloud:spring-cloud-starter-bootstrap')

That resolved my issue.

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
Dan
  • 2,052
  • 1
  • 5
  • 2
  • 6
    this doesn't work for version 3.0.3; the specified dependency is anyway already used by `spring-cloud-starter-config` – Adrian May 19 '21 at 08:53
  • 14
    For anyone still having this issue, this answer is no longer up-to-date, SpringBoot 2.4 or newer recommends using the `spring.config.import` property and there is no need to include another dependency unless your preferred way is to use bootstrap.yml. See [@Justin Cranford's](https://stackoverflow.com/a/68668500/5492826) and [@shubh gaikwad's](https://stackoverflow.com/a/67744529/5492826) answers below for more info. – emrekgn Aug 19 '21 at 13:15
  • @emrekgn this is the best solution, just a simple pom add. `spring.config.import` will show as `unknown property` and there's no way to point the config server anywhere other than `localhost:8888`. I've read the documentation and tried setting URL both ways but it will only resolve to localhost. Tried in application.yml and boostrap.yml – Philip Rego Sep 21 '21 at 19:29
  • 1
    @PhilipRego I'm not sure but maybe it was related to the version you were trying? We're currently using *spring-cloud-config-server v3.0.4* in prod env, and config-server pulls configs from some gitlab.com URL and other microservices access this config-server by setting `spring.config.import: "configserver:http://"` (not localhost) in their *application.yml*. – emrekgn Sep 22 '21 at 09:25
  • @emrekgn do I need to update my config-server? I just upgraded a client app `spring-cloud-config-client` v3.0.4 – Philip Rego Sep 22 '21 at 15:48
  • @PhilipRego Which version are you using for config-server? Maybe it'd be best if you use the same version for both of them. – emrekgn Sep 24 '21 at 15:46
  • Explanation here: https://stackoverflow.com/q/30016868/8719655 – Carlos López Marí Feb 09 '22 at 14:47
  • I had to add the version of this dependency as well. One more thing, this works for: spring-cloud-starter-netflix-zuul and not for: spring-cloud-netflix-zuul – HibaHasan May 19 '22 at 12:01
88

You're getting this error because you're using a new version of Spring Boot and Spring Cloud, but you're trying to configure it in the old way.

The Reason

Spring Cloud Config Client has changed and technically bootstrap.properties and bootstrap.yml files are deprecated.

Correct Solution

  1. Move all properties from boostrap.properties to application.properties (it can be .yml as well)
  2. Remove bootstrap.properties file
  3. Replace spring.cloud.config.uri=http://localhost:8888 with spring.config.import=configserver:http://localhost:8888

This is a proper way to tell you Spring Boot app that you want to load properties from the Spring Cloud Config service that is running on localhost:8888.

Legacy Solution

In case you want to use a legacy bootstrap.properties file, you just need to add the following dependency:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

Please note that this is a deprecated mechanism, so if you're creating a new project, go ahead with the correct solution.

Taras Boychuk
  • 2,059
  • 13
  • 21
  • 3
    Seems odd that it's deprecated when [other parts of the docs](https://cloud.spring.io/spring-cloud-config/reference/html/#:~:text=To%20modify%20the%20startup%20behavior%2C%20you%20can%20change%20the%20location%20of%20the%20config%20server%20by%20using%20bootstrap.properties) still reference it – Snekse Jun 06 '22 at 21:37
  • Hello, i tried the legacy solution and got an error : Caused by: java.lang.IllegalArgumentException: Cannot instantiate interface BootstrapRegistryInitialize : ConfigClientRetryBootstrapper – P.S Sep 28 '22 at 10:09
49

Root cause is Spring Boot 2.4 changed its default functionality. A new spring.config.import property is mandatory.

To fix, add the new spring.config.import property. Here is an example of what worked for me in application.yml.

spring:
  config:
    import: "optional:configserver:"

Here is the documentation in case you need to set a different value:

Spring Boot Config Data Import

Spring Boot 2.4 introduced a new way to import configuration data via the spring.config.import property. This is now the default way to bind to Config Server.

dur
  • 15,689
  • 25
  • 79
  • 125
Justin Cranford
  • 646
  • 5
  • 7
  • 1
    `spring.config.import` will show as `unknown property` and there's no way to point the config server anywhere other than `localhost:8888`. I've read the documentation and tried setting URL both ways but it will only resolve to localhost. Tried in application.yml and boostrap.yml – Philip Rego Sep 21 '21 at 19:29
21

Importing configuration since Spring Boot 2.4 is done by spring.config.import functionality.

Adding below to application.properties connects to the default config server URL http://localhost:8888.

spring.config.import=optional:configserver:

or yml:

spring.config.import: "optional:configserver:"

More configuration options are described in the reference documentation.

Legacy bootstrap functionality is still available if you add the org.springframework.cloud:spring-cloud-starter-bootstrap dependency to your project.

dur
  • 15,689
  • 25
  • 79
  • 125
charlb
  • 1,076
  • 9
  • 18
15

Add bootstrap.yml file:

spring:
    cloud:
      config:
        enabled: true
        uri: http://localhost:9296

where 9296 is your cloud-config server port

and add below dependency:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

If above doesn't work, simply add below properties to your application.yml file to stop the port check at place as you've already defined that in bootstrap.yml

spring:
    cloud:
      config:
        import-check:
          enabled: false
dur
  • 15,689
  • 25
  • 79
  • 125
5

I was getting this issue while implementing spring-cloud-config client. I have added bootstrap.yml to specify config server address.

Later on, added the below code in application.yml itself which resolved the issue.

spring:
   application:
    name: user-service
   config:
     import: optional:configserver:http://localhost:9004
dur
  • 15,689
  • 25
  • 79
  • 125
shubh gaikwad
  • 127
  • 2
  • 10
2

Error message itself has suggestions / action towards the solution:

Add a spring.config.import=configserver: property to your configuration.

If configuration is not required add spring.config.import= optional:configserver: instead.

To disable this check, set spring.cloud.config.enabled=false 

or

spring.cloud.config.import-check.enabled=false.
dur
  • 15,689
  • 25
  • 79
  • 125
2

I'm using Spring Cloud Config Server on port 8191 on localhost, Spring version 3.1.0 and Java 17

I have 2 answer for this

  1. Change the application.yml like bellow
spring:config:import: configserver:http://localhost:8191

In this case I don't have bootsrap.yml file

  1. Add new dependency to pom
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

And add bootstrap.yml file, my bootstrap file in that case like

spring:cloud:config:uri: http://localhost:8191
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
1

In SpringBoot v2.7.1 this solution worked for me:

My Microservice bootstrap.properties file:

spring.application.name=microservice-name
spring.cloud.config.uri=http://localhost:8888 # the URL of your server configuration

And add the following dependency:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
Malki Mohamed
  • 1,578
  • 2
  • 23
  • 40
1

If anybody is passing this error on spring-boot 2.7.0 version during tests phase put the following on src/test/application.properties

spring.cloud.config.enabled=false

If don't want to use config server for tests.

Aldo Inácio da Silva
  • 824
  • 2
  • 14
  • 38
1

Just import this dependency in pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

Note: SNAPSHOT, M1, M2, M3, and M4 releases typically WORK IN PROGRESS. The Spring team is still working on them, Recommend NOT using them.

Fazal Haroon
  • 845
  • 2
  • 10
  • 20
0

Adding this dependency resolved my issue too: spring-cloud-starter-bootstrap

0

I got past the error by removing dependency spring-cloud-starter-config. If you keep it, then it will keep prompting. I think with newer Spring Boot we do not need the dependency.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

bootstrap.properties:

spring.application.name=config-client-app
eureka.client.server-url.defaultZone=http://localhost:8761/eureka
spring.config.import=optional:configserver:

Spring version I'm working with:

<spring-cloud.version>2020.0.3</spring-cloud.version>
dur
  • 15,689
  • 25
  • 79
  • 125
xpagesbeast
  • 776
  • 1
  • 10
  • 21
  • Why did you need to remove the starter dependency? [Spring doc here](https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#_client_side_usage) specifically uses it to configure the config client. Did you maybe forget to include `spring-cloud-dependencies` bom? – emrekgn Aug 19 '21 at 13:21
0

I'm using Spring Cloud Config Server on port 8888 on localhost, Spring version 2.5.4 and Java 16

My bootstrap.properties:

spring.application.name=hr-worker
# Server Config
spring.cloud.config.enabled=true
spring.cloud.config.uri= http://localhost:8888

To stop to get error, i've just put this configuration in my application.properties:

spring.config.import=optional:configserver:http://localhost:8888
0

I got past the error using a version of spring 2.4.4 I was working in that moment with 2.5.1

application.properties:

`
spring.application.name=testserv
spring.profiles.active=prodtest
spring.config.import=optional:configserver:http://localhost:8071

spring.cloud.config.enabled=true
spring.cloud.config.uri=http://localhost:8071

management.endpoints.web.exposure.include=*

eureka.instance.preferIpAddress = true 
eureka.client.registerWithEureka = true
eureka.client.fetchRegistry = true
eureka.client.serviceUrl.defaultZone = http://localhost:8070/eureka/

`

<spring-cloud.version>2020.0.3</spring-cloud.version>

anderson j mariño o.
  • 1,739
  • 2
  • 6
  • 7
0

1- for maven add:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>

2- for gradle add:

dependencies{ implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'}

3- in application.properties replace(old configuration):

spring.cloud.config.uri=http://localhost:8888

by this:

spring.config.import=configserver:http://localhost:8888
Mounir bkr
  • 1,069
  • 6
  • 6
0

After applying any of the solutions mentioned for this question. You can give this a try:

Step 1: If on IntelliJ IDE, right-click on the 'pom.xml' file of the project.

Step 2: Find 'Maven' on the context menu. Then click on 'Reload project'.

Step 3: Next, try 'mvn clean install' and try running once again.

PS: I am still looking for the reason how it worked for me. (Maybe maven related)

sunil karki
  • 337
  • 4
  • 3
0

I am using Spring Boot 3.0.3 version, to solve this problem simply add in the application.properties file

spring.cloud.config.enabled=false

It will read the configuration from your local properties file and not from GIT repository if you have configured.

  • 1
    Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? **If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient.** Can you kindly [edit] your answer to offer an explanation? – Jeremy Caney Jul 16 '23 at 01:47
0

cloud: config: uri: http://localhost:9296(Your port no)

this works fine for me.

-1

For spring version > 2.6.6 add below property in application.property.

spring.application.name=... //name of application

spring.profiles.active=prodtest

spring.config.import=optional:configserver:http://localhost:8071

spring.cloud.config.enabled=true

spring.cloud.config.uri=http://localhost:8071

management.endpoints.web.exposure.include=*

eureka.instance.preferIpAddress = true

eureka.client.registerWithEureka = true

eureka.client.fetchRegistry = true

eureka.client.serviceUrl.defaultZone = http://localhost:8070/eureka/