Spring Boot: Passing Parameters
Spring Boot made passing parameters easy with Java annotations.
Join the DZone community and get the full member experience.
Join For FreePassing 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:
https://www.youtube.com/watch?v=tuNhqqxgxXQ&t=153s
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:
http://www.twitter.com/hashimati
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:
@GetMapping("/hello0/{name}")
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:
- Use
@RequestParam
:
@RequestParam
can be used to annotate parameters in the method’s signature. As displayed in this code snippet:
@GetMapping("/hello1")
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 parameterdefaultValue
: 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.
- 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
@GetMapping("/hello2")
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
Opinions expressed by DZone contributors are their own.
Comments