Spring Boot: Passing Parameters

DZone 's Guide to

Spring Boot: Passing Parameters

Spring Boot made passing parameters easy with Java annotations.

· Java Zone ·
Free Resource

Person kicking soccer ball

Passing parameters when requesting URLs is one of the most basic features in Spring Boot. This article shows ways to pass parameters in a request. There are two ways to pass parameters. The commonly used way to pass parameters is specifying the parameters explicitly in the URL invocation. The format will be like this:


In the above URL, there are two parameters which are v and t. To pass the parameters, put “?”. Then, add the parameter name followed by “=” and the value of the parameter. It will look like this “?v=value”. To pass another, use “&” followed by the second parameter as the first one.

The second way to pass parameter is a path variable. For example:


The word “hashimati” in this URL is a variable.

Spring Boot made passing parameters easy with Java annotations. Let's take a look at how this works in Spring Boot. 

Create a Project

Generate a Gradle or Maven project using Spring Initializer. The only dependency you need is Web.

Path Variable

In the path variable, we pass the parameter within the path of the web service. The below code snippet demonstrates a way of writing a web service that takes the path variable:

public String hello0(@PathVariable("name") String name)
    return "Hello " + name;
  • The variable name is embedded between curly brackets in the service path.
  • The name parameter is annotated with @PathVariable. We passedname in @PathParameter to indicate that the name parameter in the method signature stores the {name} value.

Request Parameter

In Spring Boot, there are two ways to pass parameters in the URL request:

  1. Use  @RequestParam:

@RequestParam can be used to annotate parameters in the method’s signature. As displayed in this code snippet:

public String hello1(@RequestParam(name="name", required = false, defaultValue = "Ahmed") String name)
return "Hello " + name;

@RequestParam has these parameters:

  • name: The name of the request parameter
  • defaultValue: The default value if the parameter is not passed in the request.
  • Required: If it’s true, the parameter is mandatory. Otherwise, it’s not.
  1. Encapsulate Parameters in an Object

Using an object is a good way to encapsulate the parameters in one object. Let’s walk through the following code snippets. First, we will create a class with name Params:

public class Parms {
    private String a, b;
    public void setA(String a) {
        this.a = a;
    public void setB(String b) {
        this.b = b;
    public String getA() {
        return a;
    public String getB() {
        return b;

The getter and setter methods should be implemented in the Param class. Then, we will use Param in the web service method’s signature

public String hello2(Parms parameters)
    //implement the setter and getter of the Params class.
    return "Hello " + parameters.a + " " + parameters.b;

And lastly, this web service takes two parameters, which are a and b.

Hope you enjoyed!

Further Reading

Using the Spring @RequestMapping Annotation

Working With Filters in Spring

spring boot

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}