CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > What is > Difference between @PathVariable and @RequestParam

Java Tutorial Menu

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

Difference between @PathVariable and @RequestParam

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: January 15, 2021 ยท 2 min read ยท 0 Comments
Share: X in ๐Ÿ”—
git change remote url

Overview

In this post, we will learn the difference between @PathVariable and @RequestParam.

These both can be used to send the value from client to server, especially in REST API call.

@PathVariable

This parameter should be bound with the API URL. Let’s say, we want to fetch the particular user’s detail then we can create an API URL like:

example.com/users/101

And the code would look like the following:

@GetMapping("/users/{id}")
@ResponseStatus(code = HttpStatus.OK)
public UserResponse getUser(@PathVariable Long id) {
	// Impl logic
}

Hence, when we have to bind our request parameter value with the URL itself then we can use the PathVariable like the above example.

Note: We have to make sure that the variable name on the method and the value for the path {id} should be the same. Otherwise, the spring does not bind the path value with the parameter in the method.

If you want to set a different path value and the method parameter then you have to define the path name explicitly so that it can be bound with the parameter.

@GetMapping("/users/{userId}")
@ResponseStatus(code = HttpStatus.OK)
public UserResponse getUser(@PathVariable("userId") Long id) {
 // here I have given the name of the path userId so that it will bind with the variable id
	// Impl logic
}

@RequestParam

We can use this annotation to send the value in the query parameter of the web request. let’s take the following example:

example.com/users?status=active

Let’s implement it in the code.

@GetMapping("/users")
@ResponseStatus(code = HttpStatus.OK)
public UserResponse getUser(@RequestParam String status) {
	// Impl logic
}

In the above example code, we haven’t added any value after /users in the @GetMapping annotation. So, spring will automatically bind the status=active sent in the URL with the variable status in getUser method.

Bonus tip

I usually prefer to use @RequestParam if there is an optional parameter to accept from the client. Otherwise, I will use the @PathVariable.

Conclusion

In this post, we learn the difference between @PathVariable and @RequestParam.

We also learn, how to use them correctly.

Related Posts:

  • Spring Data REST example.
  • Pagination and Sorting in Spring Boot
  • java.lang.Long Class in Java
  • Control Statements in Java
  • Most Frequently Asked Spring Boot Interview…
  • MySQL Commands for Developers
Tags:APIrestspring-boot
Was this article helpful?
โ† Previous ArticleHow to create Windows Task Scheduler and run a Java application?
Next Article โ†’How to solve: “The availability zones of the specified subnets and the AutoScalingGroup do not match” in CloudFormation?

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap