Fix MissingServletRequestParameterException in Spring MVC
Fix the 400 error for a missing request parameter in Spring MVC with optional parameters, defaults, correct parameter names and the right annotation.

The log line names the parameter and its type:
org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'size' for method parameter type int is not presentand the client gets a 400. A handler asked for size and the request did not carry it.
@GetMapping("/orders")
List<OrderView> list(@RequestParam int size) { ... }Cause 1: the parameter is required and absent
@RequestParam defaults to required = true. The request GET /orders fails; GET /orders?size=20 works. If the parameter is optional, say so:
@GetMapping("/orders")
List<OrderView> list(@RequestParam(defaultValue = "20") int size,
@RequestParam(required = false) String status) { ... }defaultValue implies not required and also applies when the parameter is present with an empty value. A required = false primitive such as int would fail on a missing value because a primitive cannot be null, so use Integer or a default. Optional<String> works as an optional parameter too.
Cause 2: it is in the wrong part of the request
| Where the client put it | What the handler needs |
|---|---|
Query string (?size=20) | @RequestParam |
URL path (/orders/20) | @PathVariable |
| JSON body | @RequestBody on an object |
| Header | @RequestHeader |
| Form field | @RequestParam or @ModelAttribute |
A POST with a JSON body {"size": 20} does not satisfy @RequestParam int size, because the body is not parsed into request parameters. Use a request record with @RequestBody. A form post with application/x-www-form-urlencoded does count as parameters.
Cause 3: the name does not match
Spring matches the query parameter to the method parameter name. If the client sends page_size and the method names it size, the parameter is missing. Name the binding explicitly, which also protects against renames:
@RequestParam("page_size") int sizeWithout an explicit name, Spring reads the parameter name from the compiled class, which needs the compiler flag -parameters. The Spring Boot Maven and Gradle plugins turn it on. A plain javac or a custom build may not, and then the failure is a different message:
Name for argument of type [int] not specified, and parameter name information not found in class file and 'parameters' flag not setAdd <parameters>true</parameters> to the Maven compiler plugin configuration, or use the explicit name.
Cause 4: the client sends an empty or misspelled key
?size= is present and empty; for an int it fails with a conversion error, not this one. A misspelled key such as ?sise=20 is absent. Print the query string in a request log, or use the browser's network tab.
Return a clean error
@ExceptionHandler(MissingServletRequestParameterException.class)
ProblemDetail missing(MissingServletRequestParameterException e) {
ProblemDetail p = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST,
"Missing required parameter: " + e.getParameterName());
p.setTitle("Missing parameter");
return p;
}Exposing only the parameter name is safe and useful to API clients.
What the server logs, and what the client sees
The exception is thrown during argument resolution, before your controller method runs. This endpoint requires q and defaults size. The behaviour below is from Spring Boot 4.0.9 with web logging at DEBUG.
@GetMapping("/search")
String search(@RequestParam String q, @RequestParam(defaultValue = "10") int size) {
return q + ":" + size;
}GET /search -> 400
GET /search?q=java -> 200 java:10
GET /search?q=java&size=abc -> 400
GET /search?q= -> 200Resolved [org.springframework.web.bind.MissingServletRequestParameterException:
Required request parameter 'q' for method parameter type String is not present]
Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:
Method parameter 'size': Failed to convert value of type 'java.lang.String' to required type 'int';
For input string: "abc"]Two of those rows deserve attention. A request with size=abc is also a 400, but a different exception, MethodArgumentTypeMismatchException, so searching for the wrong name leads to the wrong fix. And ?q= with an empty value returns 200. For a String, "present but empty" satisfies required = true, because the parameter exists. If an empty string should be rejected, add validation: @RequestParam @NotBlank String q on a controller marked @Validated, or check explicitly. This is a frequent source of reports that a required parameter is not required.
The default response tells the client nothing
The client received a body with timestamp, status, error and path, and no mention of which parameter was missing. Spring Boot omits the message by default so that internal text does not leak. For an API, return a body that names the field. Spring's problem detail support does it with one property.
spring.mvc.problemdetails.enabled=trueWith it on, the same request gets Content-Type: application/problem+json and a detail such as "Required parameter 'q' is not present." The log message stays as it was. When you want your own wording, handle the exception directly and name the parameter.
@RestControllerAdvice
class ParameterErrors {
@ExceptionHandler(MissingServletRequestParameterException.class)
ProblemDetail missing(MissingServletRequestParameterException e) {
ProblemDetail p = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST,
"Parameter '" + e.getParameterName() + "' of type " + e.getParameterType() + " is required.");
p.setProperty("parameter", e.getParameterName());
return p;
}
}Test it with MockMvc
A test for the failure takes a few lines and keeps the contract stable.
@WebMvcTest(SearchController.class)
class SearchControllerTest {
@Autowired MockMvc mvc;
@Test
void missingQueryIs400WithTheParameterName() throws Exception {
mvc.perform(get("/search"))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.parameter").value("q"));
}
@Test
void defaultsTheSize() throws Exception {
mvc.perform(get("/search").param("q", "java"))
.andExpect(content().string("java:10"));
}
}Check the client, not only the server
When the server is correct and the error persists, the request is wrong. Look at the exact URL in the browser's network tab or with curl -v. The common client causes are a parameter in the request body when the server reads the query string (a form post sends application/x-www-form-urlencoded, and @RequestParam does read form fields, but a JSON body is not a parameter and needs @RequestBody), a name mismatch such as userId against user_id, and a URL-building bug that drops the key when its value is null. The last one is the hardest to see, because the URL looks plausible. Print it before sending, or log the request on the client, and compare it letter by letter with the controller's annotation.
Optional and defaulted parameters, chosen on purpose
Four declarations cover the real cases, and each answers the question of what should happen when the client leaves the parameter out. @RequestParam String q makes it mandatory, with the 400 above when it is absent. @RequestParam(required = false) String q passes null when it is absent, which means the method must handle null. @RequestParam(defaultValue = "10") int size supplies a value, and it also makes the parameter not required, since a default makes absence harmless. @RequestParam Optional<String> q gives an empty Optional, which reads well in the signature and forces the method to deal with absence explicitly.
Choose the one that matches the contract of the endpoint. A search endpoint requires q. A listing endpoint defaults page and size. A filter parameter is optional and filters nothing when absent. When a parameter is both optional and meaningful in its absence, such as a date range, collect them in a record bound with @ModelAttribute, and validate the combination (both or neither) in one place, instead of scattering null checks through the method.
Where this does not apply
A value that is present and malformed gives MethodArgumentTypeMismatchException, and a body that fails constraints gives [MethodArgumentNotValidException](/library/methodargumentnotvalidexception-fix-return-clean-errors). The [@RequestParam reference](https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-controller/ann-methods/requestparam.html) covers the attributes, and the full controller design is in building REST APIs with Spring MVC. The sibling binding errors are [MethodArgumentNotValidException](/library/methodargumentnotvalidexception-fix-return-clean-errors) for validation and 405 Method Not Allowed for a wrong verb. The Spring MVC reference documents the behavior in detail.


