DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Be Punctual! Avoiding Kotlin’s lateinit In Spring Boot Testing
  • Spring Boot: How To Use Java Persistence Query Language (JPQL)
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements

Trending

  • 9 Practical Ways to Fix Laravel Performance Issues in Production
  • AI-Era Cybersecurity, Part 1: Mapping Glasswing, Daybreak, Lightwell and the Tools Racing to Find and Fix Bugs
  • DZone's Article Types
  • How Rule Engines Transform Business Agility and Code Simplicity

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Boot: Passing Parameters

Spring Boot: Passing Parameters

Spring Boot made passing parameters easy with Java annotations.

By 
Ahmed Al-Hashmi
·
Nov. 26, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
178.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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:

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:

  1. 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 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

@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

Using the Spring @RequestMapping Annotation

Working With Filters in Spring

Spring Framework Spring Boot Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Be Punctual! Avoiding Kotlin’s lateinit In Spring Boot Testing
  • Spring Boot: How To Use Java Persistence Query Language (JPQL)
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook