1

I am implementing spring config server. My config server starts fine and I can get the data from URL http://localhost:8888/client-config/development located at C:\\configprop as shown below.

{
    "name": "client-config",
    "profiles": [
        "development"
    ],
    "label": null,
    "version": "0dec53953ad4f620031cdbb3a99a0fe9701fb9df",
    "state": null,
    "propertySources": [
        {
            "name": "C:\\\\configprop/file:C:\\Users\\{user}\\AppData\\Local\\Temp\\config-repo-5116664058083487059\\client-config-development.properties",
            "source": {
                "msg": "Hello world - this is from config server - Development Environment"
            }
        }
    ]
}

but when I get the data in the config client using http://localhost:8080/message I am getting Could not resolve placeholder 'message' in value "${message}" error.

Client Application

Main Class

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

controller class

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@RestController
public class MessageRestController {

    @Value("${message}")
    private String message;

    @GetMapping("/message")
    public String getMessage() {
        return this.message;
    }
}

aplication.yml

spring:
  application:
    name: client-config
  cloud:
    config:
      uri: http://localhost:8888
  profiles:
    active: development
management:
  endpoints:
    web:
      exposure:
        include: refresh

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-client</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.0</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

client-config-development.properties

message = Hello world - this is from config server - Development Environment
SSK
  • 3,444
  • 6
  • 32
  • 59
  • do you have a 'message:this is my message' or similar in your application.properties file? that is what it's looking for – Stultuske Feb 01 '21 at 07:44
  • message property is defined in the `client-config-development.properties` – SSK Feb 01 '21 at 08:17
  • and how does that file look like? are you using other properties that are in that file, that do work? – Stultuske Feb 01 '21 at 08:32
  • @Stultuske I have added `client-config-development.properties` – SSK Feb 01 '21 at 08:49
  • so, there are no other properties in there. probably, it doesn't find your properties file. You are pointing to a fixed path: "c:\ ... " especially since you're working on a server, you should not do that. – Stultuske Feb 01 '21 at 08:58
  • I am using local repository configuration – SSK Feb 01 '21 at 09:00
  • @SSK I am also facing the same issue here, If you found any answer please ping me. Here is my question as well https://stackoverflow.com/questions/64143361/spring-cloud-config-with-database-backend – dasun_001 Feb 03 '21 at 04:55

3 Answers3

1

According to the release notes for spring cloud 2020.0, there were breaking changes.

You need to add the following to application.yml

spring.config.import="configserver:"
spencergibb
  • 24,471
  • 6
  • 69
  • 75
  • I have tried `spring.config.import=configserver:http://localhost:8888` still same issue – SSK Feb 02 '21 at 05:53
0

Hey can Inject this property by this

Step-1 Create a Class Like this and In ConfigurationProperties pass application property path a prefix

@ConfigurationProperties(prefix = "app.xyz")
@Getter
@Setter
@Configuration
public class LoginProperties {

    private String message;
    
}

At Application property

@SpringBootApplication
@EnableConfigurationProperties(LoginProperties.class)
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

In Your application.yml Property file you might have this type of value

app:
    xyz:
      message: "Hello Java"

IN Your MessageRestController class

@Autowired private LoginProperties loginProperties;

then try

 @GetMapping("/message")
    public String getMessage() {
        return loginProperties.getMessage;
    }

    

Or else you can directly try this way but am not sure

@GetMapping("/message")
    public String getMessage() {
        return `${message}`;
    }
harkesh kumar
  • 833
  • 2
  • 13
  • 35
0
@RestController
@RefreshScope
public class TestController {

    @Value("${prop1:default}")
    private String name;

    @Value("${prop2:default}")
    private String version;

    @RequestMapping(value = "/PropTest", method = RequestMethod.GET)
    public String getName() {
        return prop1+" - "+prop1;
    }
}

In bootstrap.yml add below with other properties
spring:
  config:
    import: configserver:http://localhost:8080/config-service

This worked for me. you can try this.
Partha.B
  • 51
  • 8