Quick revision
The short answer
@PathVariable binds a segment of the URI template, such as the 42 in /orders/{id}, while @RequestParam binds a query string or form parameter, such as status=NEW in /orders?status=NEW. @RequestParam is required by default, and we relax it with required = false, a defaultValue or Optional. A missing required parameter throws MissingServletRequestParameterException and a value that cannot be converted throws MethodArgumentTypeMismatchException, and Spring answers 400 Bad Request for both. A missing path variable usually just means no mapping matched, so the client gets 404.
What to remember
In this answer, we saw that @PathVariable reads segments of the URI template and identifies the resource, while @RequestParam reads query or form parameters and is required by default. We made parameters optional with required = false on wrapper types, defaultValue or Optional, and saw that a missing parameter throws MissingServletRequestParameterException and a conversion failure throws MethodArgumentTypeMismatchException, both mapped to 400. A missing path variable usually means no mapping matched and gives 404, except for a name mismatch in our own code, which gives 500.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
@PathVariable binds a segment of the URI template, such as the 42 in /orders/{id}, while @RequestParam binds a query string or form parameter, such as status=NEW in /orders?status=NEW. @RequestParam is required by default, and we relax it with required = false, a defaultValue or Optional. A missing required parameter throws MissingServletRequestParameterException and a value that cannot be converted throws MethodArgumentTypeMismatchException, and Spring answers 400 Bad Request for both. A missing path variable usually just means no mapping matched, so the client gets 404.
Why this question is asked
Binding request data to method parameters is something we do in nearly every endpoint, so the error behaviour matters a lot in practice. Clients see these errors constantly during development, and a confusing 500 instead of a clear 400 wastes everybody's time. Interviewers ask this to check that we know which part of the URL each annotation reads and what Spring does when the data is wrong. In this answer, we will recap the parts of a URL, compare the two annotations, make parameters optional, and walk through each failure case with the exception and status it produces.
1A Quick Recap of the Parts of a URL
Take the URL /customers/7/orders?status=NEW&page=2. The path is /customers/7/orders, and in our mapping it might be the URI template /customers/{customerId}/orders, where {customerId} is a placeholder for whatever is in that segment. Everything after the question mark is the query string, a list of name and value pairs. Form posts with application/x-www-form-urlencoded send the same kind of pairs in the body, and the servlet API treats both as request parameters.
The general convention is that the path identifies a resource and the query string refines what we want from it: filters, sorting, paging. That is also why one is usually required and the other usually optional.
2Comparing @PathVariable and @RequestParam
Here is one method that uses both.
| @PathVariable | @RequestParam | |
|---|---|---|
| Reads | A segment of the URI template | Query string or form parameter |
| Example | /orders/{id} -> 42 | ?status=NEW -> NEW |
| Required by default | Yes | Yes |
| Typical use | Identify the resource | Filter, sort, page |
| Missing | Usually no mapping matches (404) | 400, MissingServletRequestParameterException |
Both annotations convert the text from the request to the parameter type using Spring's conversion service, so long, int, enums, UUID and dates (with @DateTimeFormat) work out of the box. The names come from the annotation's value or name attribute, or from the method parameter name when the code is compiled with the -parameters flag, which Spring Boot builds set up for us.
3Making Parameters Optional
@RequestParam is required by default, which surprises people who expect query parameters to be optional. There are three ways to relax it, with slightly different results.
Notice the second line. With required = false and a primitive type, an absent value is null, which cannot be stored in an int, so Spring fails with an IllegalStateException that ends up as a 500. Use Integer, Optional or a defaultValue instead. A defaultValue is often the clearest choice, because the default is visible in the signature and in generated API documentation.
4What Happens When a Parameter Is Missing or Wrong
When a required @RequestParam is absent, Spring throws MissingServletRequestParameterException before calling our method. When it is present but cannot be converted, for example page=abc for an int, it throws MethodArgumentTypeMismatchException, a subclass of TypeMismatchException. Spring MVC's default exception handling maps both to 400 Bad Request, because the client sent a bad request.
- Read the parameter from the requestBy name from the query string or form
- Present?Value foundYesConvert to the parameter typeNoDefault value or optional?
- Absent: default or optional?
defaultValue, Optional, required = falseYesUse default, empty or nullNo400 MissingServletRequestParameterException - Conversion succeeds?e.g. abc to intYesCall the handlerNo400 MethodArgumentTypeMismatchException
4 steps in order. Play them, or step through them one at a time.
With spring.mvc.problemdetails.enabled=true, or with our own @RestControllerAdvice extending ResponseEntityExceptionHandler, these 400 responses get a structured problem detail body. In a custom handler, we can read the parameter name from the exception and tell the client exactly which parameter was missing or invalid.
5Why a Missing Path Variable Usually Means 404
A path variable is part of the pattern Spring uses to choose the handler. If the segment is not there, the URL usually does not match the pattern at all, so Spring never selects our method and answers 404. There is one case where we see a different error: when the mapping declares a {customerId} but the @PathVariable asks for a name that is not in the template, for example a typo like @PathVariable("customerID"). Then Spring throws MissingPathVariableException, and because this is a mistake in our code rather than the request, it maps to 500. A test for each endpoint catches this quickly.
6Follow-up questions
Can @PathVariable be optional?
It can be marked required = false or wrapped in Optional, but the mapping must then include both patterns, for example @GetMapping({"/orders", "/orders/{id}"}). In most APIs two separate methods are clearer than one method with an optional path variable.
How do we bind many query parameters at once?
Use a small record or class as a parameter without @RequestBody. Spring binds query parameters to its properties or constructor arguments by name, and we can put @Valid on it to validate them together. This keeps signatures short when an endpoint has many filters.
7Conclusion
In this answer, we saw that @PathVariable reads segments of the URI template and identifies the resource, while @RequestParam reads query or form parameters and is required by default. We made parameters optional with required = false on wrapper types, defaultValue or Optional, and saw that a missing parameter throws MissingServletRequestParameterException and a conversion failure throws MethodArgumentTypeMismatchException, both mapped to 400. A missing path variable usually means no mapping matched and gives 404, except for a name mismatch in our own code, which gives 500.
Open with
@PathVariable binds a part of the URI template, so with /orders/{id} and a request to /orders/42 it gives me 42. @RequestParam binds query string or form parameters, like status=NEW.
Then, if they want more
By convention, the path identifies the resource and query parameters filter, sort or page it. Both convert the text to the parameter type through Spring's conversion service. One thing people often miss is that @RequestParam is required by default.
To make it optional I can use a defaultValue, which I like because it's visible in the signature, or Optional, or required = false with a wrapper type, not a primitive, because null can't go into an int.
If a required parameter is missing, Spring throws MissingServletRequestParameterException before my method runs, and if the value can't be converted, like page=abc for an int, it throws MethodArgumentTypeMismatchException. Both are mapped to 400 Bad Request, and with problem details enabled the client gets a structured error that names the parameter.
A missing path variable is different: the segment is part of the mapping pattern, so if it's absent the URL usually doesn't match any handler and the client gets a 404. The exception is a name mismatch between the template and the annotation, which is a bug in my code and gives a 500.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare @PathVariable and @RequestParam. What happens when a required parameter is...": https://scrivio-interview-library.netlify.app/spring-boot/rest-controllers/3117243ded/