Required string parameter is not present

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

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;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

	@GetMapping("HelloWorld")
	public Message firstPage(@RequestParam(name = "name", required = true) String name) {
		return new Message(1, "Welcome to "+name+"!");
	}
	
}


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;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

	@GetMapping("HelloWorld")
	public Message firstPage(@RequestParam(name = "name", required = false) String name) {
		return new Message(1, "Welcome to "+name+"!");
	}
	
}


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;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

	@GetMapping("HelloWorld")
	public Message firstPage(@RequestParam(name = "name", required = false, default="unknown") String name) {
		return new Message(1, "Welcome to "+name+"!");
	}
}


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.

http://localhost:8080/HelloWorld?name=yawin



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *