Quick Fix: In Intellij IDEA, define Spring Boot configuration metadata. Use @ConfigurationProperties
-annotated class and add spring-boot-configuration-processor
dependency. Or, create spring-configuration-metadata.json
file manually.
The Problem:
IntelliJ IDEA reports that it cannot resolve Spring Boot properties, even though these properties work correctly in the application. When accessing the properties through the @Value
annotation or through an autowired Environment
, there are no issues. However, all custom-defined properties display warning messages in IntelliJ IDEA. How can these warnings be eliminated and the IDE recognize the properties?
The Solutions:
Solution 1: Adding Spring Boot Configuration Metadata
In order for IntelliJ IDEA to recognize your Spring Boot properties, you can define Spring Boot configuration metadata in your project.
Option 1: Utilizing @ConfigurationProperties
-Annotated Class
If you’re using a @ConfigurationProperties
-annotated class for your properties, you can add the Spring Boot configuration annotation processor to your classpath. This will prompt IntelliJ IDEA to generate the configuration metadata for you in the target
or out
directory.
For Maven, add the following dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
For Gradle, use the following:
implementation 'org.springframework.boot:spring-boot-configuration-processor'
Option 2: Manually Creating the Configuration Metadata File
Alternatively, you can manually create the configuration metadata file at src/main/resources/META-INF/spring-configuration-metadata.json
.
The file should contain the following content:
{
"properties": [
{
"name": "myapp.someprop",
"type": "java.lang.String"
},
{
"name": "myapp.someintprop",
"type": "java.lang.Integer"
}
]
}
Common Steps for Both Options:
- In IntelliJ IDEA, navigate to the tool window of your build system (Maven/Gradle).
- Click the "Refresh" button.
- Select "Build > Rebuild Project" from the menu.
If the warning persists, restart the IDE by selecting "File > Invalidate Caches / Restart" and clicking "Invalidate and Restart."
Solution 3: Configuration Processor for IntelliJ
If you’re experiencing the issue of IntelliJ IDEA not recognizing your Spring Boot properties, you can resolve this by adding the **Maven** dependency for the Spring Boot configuration processor:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Once you have added this dependency, follow these additional steps:
- Refresh your Maven project to download all necessary dependencies.
- Navigate to `target/classes/META-INF/spring-configuration-metadata.js` and copy the exact name of the property that you want to use.
- Ensure that your configuration class is annotated with `@ConfigurationProperties("name-here")` using the name of the property you copied in the previous step.
- Enable your configuration class by adding `@EnableConfigurationProperties(NameOfTheConfigClass.class)` to the main configuration class of your Spring Boot application.
IntelliJ should now recognize your custom Spring Boot properties and provide autocompletion and error checking for them.