by BehindJava

How to read Query String request parameters and make parameters Optional or Required in Spring Boot

Home » springboot » How to read Query String request parameters and make parameters Optional or Required in Spring Boot

In this tutorial we are going to learn about querying String request parameters and make parameters Optional or Required and return a Java Object as a return value.

@RequestParam : Annotation which indicates that a method parameter should be bound to a web request parameter.

Here we have HTTP GET request i.e. http://localhost:8080/users/123 which returns a single record with the user Id 123 and if we delete this user Id and send this HTTP GET http://localhost:8080/users request, HTTP end point should return a collection of users.

In the real time development environment, a database contains thousands of user records and returning all the records that exists in the database with a single HTTP GET request is not a good idea.

So in this case we need to implement Pagination and specify records per page by passing queries to request parameter using query String parameters. By adding question mark and request parameter name which is page i.e. page=n where n denotes page number.

The second query String parameter is being passed using the & sign i.e. limit=n, where n denotes number of records per page.

http://localhost:8080/users?page=1&limit=50

Let’s create a GET mapping to handle the above HTTP request.

Sample Code Snippet:

package com.behindjava.tutorial.controller;

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

@RestController
@RequestMapping("/users")
public class UserController {

	@GetMapping()
	public String getUser(@RequestParam(value = "page") int page, @RequestParam(value = "limit") int limit) 
	{
		return "USER'S FETCHED WITH PAGE:" + page + " and LIMIT:" + limit;
	}
}

title

Now we will continue with this tutorial in making these parameters Optional or Required to handle the HTTP request without fail by adding default value, even if one parameter is missing in the URL it can suffice the requirement with the default value. Image

Sample Code Snippet:

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

@RestController
@RequestMapping("/users")
public class UserController {

	@GetMapping()
	public String getUser(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "25") int limit) {
		return "USER'S FETCHED WITH PAGE:" + page + " and LIMIT:" + limit;
	}
}

title

Now we make our request parameter required i.e. we can provide required as true or false but it has limitations with primitive data types i.e. if you delete the default value in the request parameter and if page is not initialized in the URL then we will have an error.

“Optional int parameter “page” is present but cannot be translated into a null value due to being declared as a primitive data type”

So for this to work and also to make a best practice, required should always be used with the String in the request parameter.

Sample Code Snippet:

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

@RestController
@RequestMapping("/users")
public class UserController {

	@GetMapping()
	public String getUser(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "25") int limit,
			@RequestParam(value = "sort", required = false) String sort) {
		if (sort == null) {
			sort = "desc";
		}
		return "USER'S FETCHED WITH PAGE:" + page + " LIMIT:" + limit + " and SORT:" + sort;
	}
}

title

Previous                                                                                                               Next