Introduction
Imagine you’re filtering products on an e-commerce site. You select ORDER=ASC or ORDER=DESC from a dropdown. Behind the scenes, your backend needs to understand those values exactly as defined, not as random strings.
This is where enums shine.
Enums give your APIs structure, safety, and clarity. Instead of accepting any string, you accept only allowed values. But many beginners struggle with one common question:
How do you read enum values from query parameters in Spring Boot?
In this blog, you’ll learn how to read enum values from query parameters in Spring Boot, explained with simple analogies, clean architecture, and 100% working end-to-end Java 21 examples.
Core Concepts
What Is an Enum?
An enum is a fixed set of constants.
public enum OrderType {
ASC,
DESC
}
🧠 Analogy:
Enums are like menu options at a restaurant. You can’t order anything outside the menu.
Why Use Enums in Query Parameters?
Using enums instead of strings gives you:
✅ Compile-time safety
✅ Automatic validation
✅ Cleaner code
✅ Better API documentation
✅ Fewer bugs caused by typos
How Spring Boot Handles Enums Automatically
Spring Boot can:
- Convert query parameter strings → enum values
- Reject invalid values with
400 BAD_REQUEST - Work out-of-the-box with
@RequestParam
✔ No custom converter needed in most cases
Code Examples (End-to-End)
✅ Example 1: Reading Enum from Query Parameter (Basic & Clean)
Use Case
Sort users based on order type.
Step 1: Define Enum
package com.example.demo.enums;
public enum SortOrder {
ASC,
DESC
}
Step 2: REST Controller
package com.example.demo.controller;
import com.example.demo.enums.SortOrder;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
public class UserController {
/**
* Example URLs:
* /users?order=ASC
* /users?order=DESC
*/
@GetMapping
public String getUsers(
@RequestParam SortOrder order) {
return "Fetching users in " + order + " order";
}
}
✅ API Behavior
| Request | Result |
|---|---|
/users?order=ASC |
Success |
/users?order=DESC |
Success |
/users?order=RANDOM |
❌ 400 BAD_REQUEST |
✔ Spring automatically converts
✔ Invalid values are rejected
✔ No extra code needed
✅ Example 2: Optional Enum with Default Value (Production-Ready)
Use Case
Search products with optional sorting.
Step 1: Enum
package com.example.demo.enums;
public enum SortType {
PRICE,
RATING,
NAME
}
Step 2: REST Controller with Optional Enum
package com.example.demo.controller;
import com.example.demo.enums.SortType;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/products")
public class ProductController {
/**
* Example URLs:
* /products
* /products?sort=PRICE
* /products?sort=RATING
*/
@GetMapping
public String getProducts(
@RequestParam(
required = false,
defaultValue = "NAME"
) SortType sort) {
return "Fetching products sorted by " + sort;
}
}
✅ Behavior Explained
| URL | Result |
|---|---|
/products |
sort=NAME |
/products?sort=PRICE |
sort=PRICE |
/products?sort=INVALID |
❌ 400 BAD_REQUEST |
✔ Safe defaults
✔ Clean API
✔ Client-friendly
🧠 Bonus: Case-Insensitive Enum Handling (Optional)
By default, enums are case-sensitive.
❌ asc → error
✅ ASC → works
If you want case-insensitive support, you can add a custom converter—but avoid this unless required, as enums are meant to be strict.
Best Practices
Always use enums for fixed values
Avoid accepting raw strings for known options.Use default values for optional enums
Prevent unnecessary client errors.Keep enum names API-friendly
PreferASCoverASCENDING_ORDER_TYPE.Do not expose internal enums blindly
Enums are part of your public API—design them carefully.Document allowed enum values
Makes APIs easier to consume and test.
Common Mistakes to Avoid
❌ Using String instead of enum
❌ Allowing too many enum values
❌ Changing enum values without versioning
❌ Catching enum conversion errors in controllers
❌ Ignoring API backward compatibility
Conclusion
Reading enum values from query parameters is one of the cleanest and safest patterns in Spring Boot.
By leveraging Spring Boot’s built-in conversion:
- You write less code
- You get validation for free
- Your APIs become more expressive and robust
Mastering reading enum values from query parameters in Spring Boot will significantly improve your Java programming skills and help you learn Java the right way—using real-world, production-ready practices.
Call to Action
💬 Have questions about enums, validation, or API design?
👇 Drop them in the comments below!
Want next topics?
- Enum validation with custom error messages
- Enum mapping with
@Validated - API versioning with enums
Just ask—happy to help 🚀
🔗 Helpful Resources
Top comments (0)