<p>I have mounted one volume which contained username and password inside pod. If I do:</p>
<pre><code>kubectl  exec -it my-app -- cat /mnt/secrets-store/git-token

{&quot;USERNAME&quot;:&quot;usernameofgit&quot;,&quot;PASSWORD&quot;:&quot;dhdhfhehfhel&quot;}
</code></pre>
<p>I want to read this USERNAME and PASSWORD using Spring Boot.</p>

Chintamani
<p>Assuming:</p>
<ul>
<li>the file (git_token) format is fixed (JSON).</li>
<li>the file may not have an extension suffix (.json).</li>
</ul>
<p>... we have some Problems!</p>
<p>I tried <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.files.importing-extensionless" rel="nofollow noreferrer">2.3.5. Importing Extensionless Files</a> like:</p>
<pre><code>spring.config.import=/mnt/secrets-store/git-token[.json]
</code></pre>
<p>But it works only with YAML/.properties yet!(tested with spring-boot:2.6.1))</p>
<p>Same applies to <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.typesafe-configuration-properties" rel="nofollow noreferrer">2.8. Type-safe Configuration Properties</a>. ;(;(</p>
<hr />
<p>In Spring-Boot we can (out-of-the box) provide JSON-config (only) as <code>SPRING_APPLICATION_JSON</code> environment/command line property, and <em>it has to be the json string</em>, and cannot be a path or file (yet).</p>
<hr />
<p>The proposed (baeldung) article shows ways to &quot;enable JSON properties&quot;, but it is a long article with many details, shows much code and has decent lacks/outdates (@Component on @ConfigurationProperties is rather &quot;unconventional&quot;)..</p>
<hr />
<p>I tried the following (on local machine, under the mentioned assumptions):</p>
<pre class="lang-scala prettyprint-override"><code>package com.example.demo;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {

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

  @Value(&quot;&quot;&quot;
    #{@jacksonObjectMapper.readValue(
      T(java.nio.file.Files).newInputStream(
        T(java.nio.file.Path).of('/mnt/secrets-store/git-token')),
        T(com.example.demo.GitInfo)
    )}&quot;&quot;&quot; // watch out with @Value and text blocks! (otherwise: No converter found capable of converting from type [com.example.demo.GitInfo] to type [java.lang.String])
  )
  GitInfo gitInfo;

  @Bean
  CommandLineRunner runner() {
    return (String... args) -&gt; {
      System.out.println(gitInfo.getUsername());
      System.out.println(gitInfo.getPassword());
    };
  }
}

@Data
class GitInfo {
  @JsonProperty(&quot;USERNAME&quot;)
  private String username;
  @JsonProperty(&quot;PASSWORD&quot;)
  private String password;
}
</code></pre>
<p>With (only) spring-boot-starter-web and lombok on board, it prints the expected output.</p>
<hr />
<p>Solution outline:</p>
<ul>
<li>a pojo for this
<ul>
<li>the upper case is little problematic, but can be handled as shown.</li>
</ul>
</li>
<li>a (crazy) <code>@Value</code> - (Spring-)Expression, involving:
<ul>
<li>(hopefully) auto-configured <code>@jacksonObjectMapper</code> bean. (alternatively: custom)</li>
<li><a href="https://fasterxml.github.io/jackson-databind/javadoc/2.13/com/fasterxml/jackson/databind/ObjectMapper.html" rel="nofollow noreferrer">ObjectMapper#readValue</a> (alternatives possible)</li>
<li><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/Files.html#newInputStream(java.nio.file.Path,java.nio.file.OpenOption...)" rel="nofollow noreferrer">java.nio.file.Files#newInputStream</a> (alternatives possible)</li>
<li><a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/Path.html#of(java.lang.String,java.lang.String...)" rel="nofollow noreferrer">java.nio.file.Path#of</a></li>
</ul>
</li>
</ul>
