How to deal with optional request parameters in Spring Boot?

Let’s say you are creating a REST API in Spring Boot which accepts request parameters.

You use the annotation @RequestParam to retrieve the request values.

And if user does not supply the request parameter Spring will throw error.

What if the request parameter is just an optional parameter?

How to handle this in Spring ?

You can do it in three ways:

  1. Set required = false in @RequestParam annotation
  2. Set defaultValue = “any default value” in @RequestParam annotation
  3. Using Optional keyword

For example , consider the below REST API :

package com.example.restapis;

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("/requestparamtest")
	public String test(@RequestParam("message") String message) {
		
		return message;
		
	}
}

It accepts a request parameter message and returns it.

Let’s test it:

And when you don’t enter the message request parameter , Spring throws error:

Let’s make this optional using the three ways cited earlier

1)Using required = false in @RequestParam annotation:

Here is the code:

package com.example.restapis;

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("/requestparamtest")
	public String test(@RequestParam(name="message",required=false) String message) {
		
		return message;
		
	}
}

Here is the output :

Since message is not passed null value is returned and the browser treats it as no response.

Advertisements

2) Use defaultValue = “any default value” in @RequestParam annotation:

Here is the code:

package com.example.restapis;

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("/requestparamtest")
	public String test(@RequestParam(name="message",defaultValue="Hello World") String message) {
		
		return message;
		
	}
}

And here is the output:

No request parameter is passed , still Spring doesn’t throw error now , it returns the default value.

Advertisements

3) Using Optional keyword

You can wrap the request parameter using Optional keyword and prevent Spring from throwing exception:

package com.example.restapis;

import java.util.Optional;

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("/requestparamtest")
	public String test(@RequestParam(name = "message") Optional<String> message) {

		return message.isPresent() ? message.get() : null;

	}
}

Response:

You can also return a default value here while checking if the optional value is empty:

package com.example.restapis;

import java.util.Optional;

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("/requestparamtest")
	public String test(@RequestParam(name = "message") Optional<String> message) {

		return message.isPresent() ? message.get() : "Hello Optional";

	}
}

The response:

That’s it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s