Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 15

    Path Variables & Request Params

    Orders identifiers belong in the path (/api/orders/42).

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Orders identifiers belong in the path (/api/orders/42). Filters, sort, and pagination belong in the query string (?status=PENDING&page=0&size=20).

    @PathVariable binds URI template variables. @RequestParam binds query parameters (and form fields). Use required, defaultValue, and Jakarta constraints such as @Max on page size.

    Type conversion is built in for primitives, enums, and dates with standard formats. Custom converters belong in configuration — not ad-hoc parsing in every controller.

    Unbounded size parameters are a denial-of-service footgun against the database.

    Purpose of this lesson

    Bind Orders path ids and list filters correctly, with safe defaults and caps on page size.

    Understanding the topic

    Path means identity; query means options.

    • @PathVariable: required pieces of the resource URI.
    • @RequestParam: optional filters and paging.
    • defaults: page=0, size=20 so /api/orders works bare.
    • validation: @Max(100) on size; validate enums via type conversion failures or explicit checks.
    • names: keep query names stable; they become client contracts.

    Step-by-step explanation

    1. Implement GET /{id} with @PathVariable long id.
    2. Implement GET /api/orders with optional status and paging params.
    3. Cap size with @Max(100) and enable method validation if needed.
    4. Curl with and without query params; confirm defaults.

    Informative example

    Get one + search.

    java
    @GetMapping("/{id}")
    OrderView getOne(@PathVariable long id) {
    return orders.get(id);
    }
    @GetMapping
    List<OrderView> search(
    @RequestParam(required = false) OrderStatus status,
    @RequestParam(defaultValue = "0") int page,
    @RequestParam(defaultValue = "20") @Max(100) int size) {
    return orders.search(status, page, size);
    }

    Real-world use

    Instructor ops lists PENDING Orders while mobile opens a single id. Mixing filters into path segments (/orders/PENDING) collides with numeric ids and breaks caching. Query params keep identity URLs clean.

    Security implications

    IDs in paths are still authorization problems.

    • Check the caller may access order id — do not trust obscurity.
    • Limit page size to protect DB CPU and memory.
    • Do not reflect raw query strings into logs without redaction.

    Best practices

    • Document default page size in the API guide.
    • Prefer enums for status query params.
    • Keep path variable names aligned with URI templates for readability.

    Common mistakes

    • Using query params for the primary resource id.
    • Allowing size=100000 without a max.
    • Silent stringly-typed status filters that ignore typos instead of 400ing.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionPath variable vs request param — when each?+

    Answer

    Path variables identify the resource; query parameters filter, sort, or paginate representations of a collection.
    2IntermediateQuestionHow do you prevent huge page sizes?+

    Answer

    Default size, annotate with @Max, and enforce the same cap in the service even if a caller bypasses the controller.
    3IntermediateQuestionWhat happens when a query enum value is invalid?+

    Answer

    Spring's conversion fails and typically yields 400 Bad Request before your method runs — better than treating unknown status as empty success.

    Hands-on exercise

    Lab. Add search with default paging and @Max(100). Call with size=1000 and confirm validation fails once method validation is enabled.

    Summary

    Orders binds identity and filters at the controller edge with safe defaults. Next you align those handlers with REST resource semantics.

    Ready to mark this lesson complete?Track your journey across the entire course.