The spring boot error Required string parameter is not present occurs when the request parameter is not stated in the request url and the request parameter is configured as mandatory. The controller method needs to provide a value for the request parameter. The invoked url does not contain the value of the request parameter. If the value for the @RequestParam is not given, the exception Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter ‘name’ is not present] will be thrown.
The request param is configured using the annotation @RequestParam in spring boot. By default, the value of the @RequestParam annotation is mandatory.
In a rest call, request parameter can be made as mandatory by setting required=true. If a parameter is mandatory and the parameter value is not received by restcontroller, then this warning message is thrown in the application. Browser shows as There was an unexpected error (type=Bad Request, status=400).
Exception
2019-10-03 17:40:07.516 WARN 4304 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'name' is not present]
Error Message
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Thu Oct 03 17:42:47 IST 2019 There was an unexpected error (type=Bad Request, status=400). Required String parameter 'name' is not present
How to reproduce this issue
In Spring Boot Application, create a rest controller class. Create a method with a @Requestparam as a parameter in the rest controller class. Configure the request param as required=true. Now call this rest url without passing value to this request param.
package com.yawintutor.application;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("welcome")
public String firstPage(@RequestParam(name = "name", required = true) String name) {
return new String( "Welcome to "+name+"!");
}
}
Output
$ curl -X GET http://localhost:8080/welcome?username=Yawin
{"timestamp":"2020-12-30T22:50:10.246+0000","status":400,"error":"Bad Request","message":"Required String parameter 'name' is not present","path":"/welcome"}
Root Cause
The request url does not contain the value of the request parameter. The request parameter is configured as the required parameter in the spring boot rest controller. When the request call to the controller method is reached, the error Required string parameter is not present is thrown as the required request parameter is not available.
Solution 1
The request parameter is added using the @Requestparam annotation. If the “required” attribute of the @RequestParam annotation is configured as true, the controller method expects the value for the request param. If the param value is an optional value, configure as required=false in the @RequestParam annotation.
In the example below, the required attribute is set to false so that the request parameter value is optional.
package com.yawintutor.application;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("welcome")
public String firstPage(@RequestParam(name = "name", required = false) String name) {
return new String( "Welcome to "+name+"!");
}
}
Output
In this case, the value of the request param ‘name’ is null.
$ curl -X GET http://localhost:8080/welcome?username=Yawin
Welcome to null!
Solution 2
When the rest controller method requires a request param value and the request url can not send a request parameter, the @RequestParam annotation allows the default value to be configured. If the url has the value of the request param, the value will be used in the method. If the value of the request param is not passed in the url, the default value is used in the method.
package com.yawintutor.application;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("welcome")
public String firstPage(@RequestParam(name = "name", required = false, defaultValue="unknown") String name) {
return new String( "Welcome to "+name+"!");
}
}
Output
$ curl -X GET http://localhost:8080/welcome?username=Yawin
Welcome to unknown!
Solution 3
The request param value is configured as required in the @RequestParam annotation of the rest controller method. The value of the request param must be sent to the url. The request param should be passed to the url as the key = value format.
The example below shows how to configure the request param in the url request.
$ curl -X GET http://localhost:8080/welcome?name=Yawin
Welcome to Yawin!