Bind Case Insensitive @Value to Enum in Spring Boot
In Spring Boot, the @Value
annotation is commonly used to inject values from property files into variables. When binding these values to Enum
fields, the process is case-sensitive by default. This can be problematic if you want flexibility in how values are defined in property files. For example, you may want to allow lowercase or mixed-case input to map to uppercase enum constants.
In this article, we will explore how to bind a case-insensitive @Value
to an Enum
in Spring Boot by implementing a custom Converter
.
Default Case-Sensitivity in Enum Binding
By default, Spring Boot’s @Value
does not handle case-insensitive binding between Strings and Enums. This can result in an IllegalArgumentException
if the case of the string in the properties file does not match the case of the enum constant.
For example:
public enum MyEnum {
OPTION_ONE,
OPTION_TWO
}
And in your application.properties
:
app.enum-value=option_one
This will throw an exception because option_one
does not match OPTION_ONE
. To handle this case-insensitively, we need to implement a custom converter.
Spring Boot Default Behavior with Enum Binding
Without customization, the following code works only when the property value matches the enum case exactly:
For example:
@Value("${app.enum-value}")
private MyEnum enumValue;
- If the property value is
OPTION_ONE
, it will work. - If the property value is
option_one
orOption_One
, it will throw anIllegalArgumentException
.
The Need for Case-Insensitive Enum Binding
To allow case-insensitive mapping between property strings and enums, the solution requires two components:
- Custom Converter: A Spring
Converter
that transforms a string into an enum, ignoring the case. - Component Registration: Register the custom converter as a Spring component so Spring can automatically use it during property injection.
Implementation to Bind Case Insensitive @Value to Enum in Spring Boot
To bind a case-insensitive @Value
to an Enum in a Spring project, we need to ensure that Spring can handle the conversion between the String and the target Enum. By default, Spring's @Value
binding is case-sensitive when mapping to Enums. To address this, we will create a custom converter.
Step 1: Create a New Spring Boot Project
Create a new Spring Boot project using IntelliJ IDEA. Choose the following options:
- Name: CaseInsensitiveEnum-Demo
- Language: Java
- Type: Maven
- Packaging: Jar
Click on the Next button.

Step 2: Add the Dependencies
Add the following dependencies to your Spring Boot project:
- Spring Web
- Spring Boot DevTools
- Lombok
Click on the Create button.

Project Structure
Once the project is created, the file structure will look like the image below.

Step 3: Configure Application Properties
Open the application.properties
file and add the value that will be case-insensitively bound to the Enum in the Spring application.
spring.application.name=CaseInsensitiveEnum-Demo
app.enum-value=option_one
Step 4: Create the CaseInsensitiveEnum
Class
Create a class named CaseInsensitiveEnum.java
and add the following code.
CaseInsensitiveEnum.java:
package com.gfg.caseinsensitiveenumdemo;
public enum CaseInsensitiveEnum {
OPTION_ONE,
OPTION_TWO,
OPTION_THREE
}
Step 5: Create the Custom Enum Converter
Create a class named EnumConverter
that will handle the case-insensitive conversion for the Enum in the application.
EnumConverter.java:
package com.gfg.caseinsensitiveenumdemo;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
@Component
public class EnumConverter implements Converter<String, CaseInsensitiveEnum> {
@Override
public CaseInsensitiveEnum convert(String source) {
// Case-insensitive conversion to Enum
try {
return CaseInsensitiveEnum.valueOf(source.toUpperCase());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid value for enum: " + source);
}
}
}
Step 5: Controller
Create the EnumController class and this class can demonstrates the how to bind the case-insensitive value to the Enum.
EnumController.java
package com.gfg.caseinsensitiveenumdemo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class EnumController {
@Value("${app.enum-value}")
private CaseInsensitiveEnum enumValue;
@GetMapping("/enum")
public String getEnumValue() {
return "Enum value from @Value: " + enumValue;
}
}
Step 6: Main Class
No changes are required in the main class.
package com.gfg.caseinsensitiveenumdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CaseInsensitiveEnumDemoApplication {
public static void main(String[] args) {
SpringApplication.run(CaseInsensitiveEnumDemoApplication.class, args);
}
}
pom.xml File:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://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>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gfg</groupId>
<artifactId>CaseInsensitiveEnum-Demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CaseInsensitiveEnum-Demo</name>
<description>CaseInsensitiveEnum-Demo</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 7: Run the Application
Once the project is completed, it will run and start on port 8080.

Step 8: Testing the Endpoint
Now, we will test the below endpoint using postman tool.

This example project demonstrates how to create the Spring Boot project where the @Value can bind to the Enum case-insensitively by using the custom Converter.
Conclusion
In this article, we demonstrated how to bind the case-insensitive @Value
to an Enum in Spring Boot. We achieved this by creating a custom Converter that handles the case conversion, ensuring that the string value from the properties file can be matched to the enum value regardless of case. This approach adds flexibility to how enum values are specified in the configuration files.