How Do You Read Enum Values from Query Parameters in Spring Boot?

Published: (December 26, 2025 at 03:30 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

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. Many beginners wonder:

How do you read enum values from query parameters in Spring Boot?

In this article you’ll learn how to read enum values from query parameters in Spring Boot, with clean architecture and working 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 is 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

RequestResult
/users?order=ASC200 OK (success)
/users?order=DESC200 OK (success)
/users?order=RANDOM400 BAD_REQUEST

Spring automatically converts valid values and rejects invalid ones.

✅ 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

URLResult
/productssort=NAME (default)
/products?sort=PRICEsort=PRICE
/products?sort=INVALID400 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 need case‑insensitive support, add a custom Converter. Use this only when required, as enums are intended to be strict.

Best Practices

  • Always use enums for fixed values – avoid raw strings.
  • Provide default values for optional enums to prevent unnecessary client errors.
  • Keep enum names API‑friendly (e.g., ASC rather than ASCENDING_ORDER_TYPE).
  • Do not expose internal enums blindly – they become part of your public API.
  • Document allowed enum values – makes APIs easier to consume and test.

Common Mistakes to Avoid

  • Using String instead of an enum for known options.
  • Defining too many enum values, causing API bloat.
  • Changing enum values without versioning the API.
  • Catching enum conversion errors inside controllers (let Spring handle them).
  • Ignoring backward compatibility when evolving enums.

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
  • Get validation for free
  • Make your APIs more expressive and robust

Mastering this technique will 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!

Suggested next topics

  • Enum validation with custom error messages
  • Enum mapping with @Validated
  • API versioning with enums

Just ask—happy to help 🚀

Helpful Resources

Back to Blog

Related posts

Read more »

Basic CRUD using Java Spring Boot

What is Spring Boot Spring Boot is a Java framework that helps you build web and backend applications easily. It takes care of most of the setup and configurat...