Why Your Spring Boot DeepSeek API Key Stays Old and How to Fix It

In Spring Boot projects, configuration values can appear unchanged due to environment variable precedence, misplaced files, missing setters, profile activation issues, or IDE caching, and this guide walks through five common pitfalls with concrete commands and code snippets to resolve them.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Why Your Spring Boot DeepSeek API Key Stays Old and How to Fix It

Background: The "Stale" DeepSeek API Key Issue

When integrating the DeepSeek API into a Spring Boot application, the property deepseek.api-key was updated in application.properties, but the running code kept reading the old key, resulting in a 401 error ( API key: ****307b is invalid).

Investigation revealed that an environment variable deepseek_api_key=sk-旧密钥 was overriding the property because Spring Boot gives higher priority to environment variables.

Spring Boot Configuration Loading Priority

Spring Boot loads configuration sources in a specific order (high to low). Higher‑priority sources overwrite lower‑priority ones. The following diagram illustrates the hierarchy:

Configuration priority diagram
Configuration priority diagram

Five Common Pitfalls and Their Solutions

Pitfall 1: Environment Variable Overrides Configuration File

Problem : application.properties contains the new key, but the console prints the old key.

Cause : An OS environment variable deepseek_api_key is automatically mapped to deepseek.api-key and takes precedence.

Solution :

Windows: echo %deepseek_api_key% then set deepseek_api_key= to delete.

Linux/Mac: echo $deepseek_api_key then unset deepseek_api_key.

After removal, restart the application to verify the new value is loaded.

Pitfall 2: Configuration File Placed in Wrong Path

Problem : The file is under src/main/java/com/example/config, so Spring Boot cannot find it and uses default values.

Cause : Spring Boot only scans src/main/resources for application.properties or application.yml.

Solution :

Move application.properties to src/main/resources.

Project structure example:

src
 └─ main
     ├─ java      # source code
     └─ resources  # configuration files
         └─ application.properties

If a custom location is required, specify it in the startup command, e.g.:

@SpringBootApplication
public class QuestionnaireBackendApplication {
    public static void main(String[] args) {
        SpringApplication.run(QuestionnaireBackendApplication.class,
            "--spring.config.location=classpath:/config/application.properties");
    }
}

Pitfall 3: Configuration Class Lacks Setter Methods

Problem : DeepSeekConfig reads apiKey as null or default.

Cause : @ConfigurationProperties(prefix = "deepseek") requires JavaBean setters to inject values.

Solution : Add setters for all fields. Example:

@Configuration
@ConfigurationProperties(prefix = "deepseek")
public class DeepSeekConfig {
    private String apiKey;
    private String apiUrl = "https://api.deepseek.com/chat/completions";
    private String model = "deepseek-chat";
    private int timeout = 30000;

    public void setApiKey(String apiKey) { this.apiKey = apiKey; }
    public void setApiUrl(String apiUrl) { this.apiUrl = apiUrl; }
    public void setModel(String model) { this.model = model; }
    public void setTimeout(int timeout) { this.timeout = timeout; }

    @PostConstruct
    public void printConfig() {
        System.out.println("=== DeepSeek configuration ===");
        System.out.println("apiKey (masked): " + (apiKey == null ? "null" : apiKey.substring(0,6) + "****"));
        System.out.println("apiUrl: " + apiUrl);
        System.out.println("model: " + model);
    }
}

Pitfall 4: Wrong Profile Activation Causes Old Values

Problem : application-dev.properties contains an old key and overrides the main configuration.

Cause : spring.profiles.active=dev activates the dev profile, giving it higher priority.

Solution :

Remove or change the active profile if the main file should be used.

Edit the appropriate profile file ( application-dev.properties) with the correct key.

Alternatively, specify the profile at launch:

java -jar questionnaire-backend.jar --spring.profiles.active=prod

Pitfall 5: IDE Cache Leads to Stale Configuration

Problem : After editing deepseek.api-key, the application still reads the old value.

Cause : IDE (e.g., IntelliJ IDEA) caches compiled classes; the updated property file is not re‑compiled.

Solution :

Rebuild the project: IDEA → Build → Rebuild Project; Eclipse → Clean → Build Project.

Invalidate caches: IDEA → File → Invalidate Caches… → Invalidate and Restart.

Quick Validation: 3‑Line Snippet to Verify Configuration

@PostConstruct
public void printConfig() {
    System.out.println("=== Configuration Load Result ===");
    String maskedApiKey = apiKey == null ? "null (not loaded)" : apiKey.substring(0,6) + "****" + apiKey.substring(apiKey.length()-4);
    System.out.println("deepseek.api-key: " + maskedApiKey);
    System.out.println("deepseek.api-url: " + apiUrl);
    System.out.println("deepseek.model: " + model);
}

null → Configuration not injected (check setters, file location).

old value → Environment variable, command‑line argument, or active profile overriding.

new value → Configuration loaded successfully; further issues may lie elsewhere.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

configurationSpring BootDeepSeekAPI keyenvironment-variable
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.