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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • Using Java Stream Gatherers To Improve Stateful Operations
  • Implementing Explainable AI in CRM Using Stream Processing
  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • AI Agents: A New Era for Integration Professionals
  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 user avatar
Ahmed Al-Hashmi
·
Nov. 26, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
176.9K Views

Join the DZone community and get the full member experience.

Join For Free

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
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!