Spring Boot @RequestParam tutorial
last modified July 6, 2020
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:
- defaultValue - used as a fallback when the request parameter is not provided or has an empty value
- name - name of the request parameter to bind to
- required - tells whether the parameter is required
- value - alias for name
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
.
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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>requestparamex</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
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; @Controller public class MyController { @RequestMapping(path="/message", produces=MediaType.TEXT_PLAIN_VALUE) @ResponseBody public String processForm(@RequestParam(defaultValue="Guest") String name, @RequestParam(required = false) String adult) { var greet = "on".equals(adult) ? "Good morning" : "Hi"; return String.format("%s %s!", greet, name); } }
The controller processes the HTML form. It reads two parameters from the request.
@Controller public class MyController {
A controller class is annotated with the @Controller
annotation in Spring.
@RequestMapping(path="/message", produces=MediaType.TEXT_PLAIN_VALUE) @ResponseBody
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.
var greet = "on".equals(adult) ? "Good morning" : "Hi"; return String.format("%s %s!", greet, name);
We build the message and return it.
<!DOCTYPE html> <html lang="en"> <head> <title>Home page</title> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> </head> <body> <form action="message"> <div> <label>Name:</label> <input type="text" name="name"> </div> <div> <label><input type="checkbox" name="adult">Adult</label> </div> <button type="submit">Submit</button> </form> </body> </html>
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.
package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Application
is the entry point which sets up Spring Boot
application. The @SpringBootApplication
annotation enables
auto-configuration and component scanning.
$ mvn spring-boot:run
After the application is run, we can navigate to localhost:8080
.
In this tutorial, we have created web application with Spring Boot framework.
We have demonstrated the usage of @RequestParam
.