Spring Boot @RequestParam tutorial

In this tutorial, we are going to use the @RequestParam annotation in a controller to read request parameters.

Spring is a popular Java application framework and Spring Boot is an evolution of Spring which helps create stand-alone, production-grade Spring based applications easily.

Spring @RequestParam

@RequestParam is a Spring annotation used to bind a web request parameter to a method parameter.

It has the following optional elements:

Spring @RequestParam example

The following example creates a Spring Boot web application which uses @RequestParam. We have an HTML form with two tags: text input and check box. These two tags create request parameters that are read in the controller with @RequestParam.

$ tree
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── zetcode
    │   │           ├── Application.java
    │   │           └── controller
    │   │               └── MyController.java
    │   └── resources
    │       └── static
    │           └── index.html
    └── test
        └── java

This is the project structure of the Spring Boot application.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 

In the the Maven build file we have the spring-boot-starter-web, which is a starter for building web applications using Spring MVC. It uses Tomcat as the default embedded container. The spring-boot-devtools is an artifact useful when developing Spring Boot applications; it allows automatic restart or live reload of applications. The application is packaged into a JAR file.

package com.zetcode.controller;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

public class MyController {

    @RequestMapping(path="/message", produces=MediaType.TEXT_PLAIN_VALUE)
    public String processForm(@RequestParam(defaultValue="Guest") String name, 
            @RequestParam(required = false) String adult) {
        String greet = "on".equals(adult) ? "Good morning" : "Hi"; 
        String message = String.format("%s %s!", greet, name);
        return message;

The controller processes the HTML form. It reads two parameters from the request.

public class MyController {

A controller class is annotated with the @Controller annotation in Spring.

@RequestMapping(path="/message", produces=MediaType.TEXT_PLAIN_VALUE)

The processForm() method is mapped to the /message path and returns plain text. The @ResponseBody annotation indicates that the method return value is bound to the web response body.

public String processForm(@RequestParam(defaultValue="Guest") String name, 
        @RequestParam(required = false) String adult) {

With the @RequestParam annotation, we bind the request parameter to the method variable. The defaultValue option gives a default value if the parameter is not available (the text input was left empty). The required option tells that the parameter is required. The method retuns a string.

String greet = "on".equals(adult) ? "Good morning" : "Hi"; 
String message = String.format("%s %s!", greet, name);

return message;

We build the message and return it.

<!DOCTYPE html>
<html lang="en">
        <title>Home page</title>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>

        <form action="message">

                <input type="text" name="name">

                <label><input type="checkbox" name="adult">Adult</label>    

            <button type="submit">Submit</button>


The index.html file is the home page. The file is located in the src/main/resources/static directory, where Spring Boot expects static resources such as HTML or CSS files. We have a simple HTML form with input text and check box tags.

<form action="message">

The action option contains a string that is used in controller method mapping.

In this tutorial, we have created web application with Spring Boot framework. We have demonstrated the usage of @RequestParam. You might also be interested in the related tutorials: Spring Boot @PathVariable tutorial, Spring Boot @ResponseBody tutorial, Spring Boot REST H2 tutorial, Standalone Spring applications, Java tutorial.