EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot Path Variable
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

Spring Boot Path Variable

Spring Boot Path Variable

Definition of Spring Boot Path Variable

In Spring boot application, we have path variable which sent in the URL. In spring boot it is an annotation that tells us that this parameter will be sent in the URI only, we have to follow the proper syntax and standard defined by the spring boot framework. We should also have the required dependency in place, in order to use this in our project of spring boot. While sending this data to spring boot application the parameter should also be correct otherwise it will throw some error. In the coming section of the tutorial, we will see the step-by-step guide to using this in our application, also the required configuration to use this in the program for beginners.

Syntax:

In this section, we will see the syntax for the path variable in spring boot, as we already know that they used to map the parameter from the URI in spring. Let’s take a closer look at the syntax for the path variable for better understanding see below;

@RequestMapping(path="/{your path}/{your path}")
public String getMessage(@PathVariable("variable_name") String variable,
@PathVariable("variable_name") String variable) {
// logic will go here
}

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

As you can see in the above syntax for path variable we have used the @PathVariable annotation here to represent the variable as the path variable. Also, let’s take a sample piece of code to understand the usage of path variable in detail see below;

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,754 ratings)

e.g. :

@RequestMapping(path="/{id}")
public String demo(@PathVariable("id") int id) {
// logic will go here
}

As from the above piece of code it is simple to understand and implement this. In the coming section of the tutorial, we will see how we can use this while programming for beginners.

How path variable works in Spring boot?

As of now we already know that path variable is used to specify the variable in the request URI, also we have to follow the standard and syntax defined by the spring boot framework in order to get the value at controller, otherwise, it will throw us runtime exception saying 417 or 404. In order to use the path variable, we will first see the syntax and then sample a piece of code to test and run it in our application, we can test or URL from the postman or any tool, or from UI also if you have one.

1) Use: this is a simple annotation in the spring boot framework, which can be used with the method parameter in order to get the value from the URL. We may have some cases where it is not always feasible to send the object to the controller sometime we may require to get the data based on some specific params only, then we can go for the path variable. Also, it does not require any configuration to use this they are just like any other annotation in the spring boot framework.

Syntax:

@PathVariable

2) How to use this: in order to use this, we need to have the following dependency in place, for the spring boot application. We should have the web dependency for spring boot in place because this parameter usually related to the URI only. Below you can find the package name where we can find it see below;
e.g. :

org.springframework.web.bind.annotation;

We have to have this package in place in order to use this. After this, we can directly use this annotation in our project.

3) Over method: We can use this annotation with the method parameter, the required parameter should be annotated using this annotation in spring boot. We will see one syntax of how we use this while programming in spring boot;

e.g:

@RequestMapping(path="/{rollno}")
public String demo(@PathVariable("rollno") int rollno) {
// logic will go here
}

As you can see in the above piece of code we have specified some of the value with it lets discuss each of them in detail see below;

a) required: This is the most common and frequently used option with the path variable in spring boot. It tells that this parameter is required in the URL, if not present it will throw an error for us. IT can act as a validation also for us if we want to validate the value before use.

b) name: This is also an optional value for the path variable in spring boot by the use of it we can tell which parameter we need to bind its value too.

c) value: By the use of it, we can specify an alias for the parameter we have created to access it by that name.

4) how to call the URL: After making all the changes to the code, now we have to call the URL and pass some parameters inside it. so for that wit should be separated by the forward-slash in the URL and the value for the parameters. Remember it should be in the same order that you have created in the controller, which we will see in detail in the example section.
Steps;

1) Make a simple project from the spring initializer and try to do the following changes inside it.

2) @SpringBootApplication
public class DemoApplication {
public static void main(String[] args) throws IOException {
SpringApplication.run(DemoApplication.class, args);
}
}

3) After that make one controller and make on the method which takes path variable as the parameter

4) @RestController
@CrossOrigin
@RequestMapping("/employee/")
public class EmployeeController {
@GetMapping("get/{name}/{rollno}")
public String getData(@PathVariable String name, @PathVariable String rollno) {
return // our logic ..//
}
}

5) and inside the service class, we can perform the required logic to our code to get the desired output. from the db or any other logic.

Conclusion

By the use of the path variable in spring boot, we can bind our variable to the request URL. This is very easy to sue and handle that we have already seen in the above example and piece of code. But to fully test and configure t we have to make an application from scratch to test it with all necessary configuration inside it.

Recommended Articles

This is a guide to Spring Boot Path Variable. Here we discuss definition, syntax, and parameters, How path variable works in Spring boot? examples with code implementation. You may also have a look at the following articles to learn more –

  1. Spring Boot DevTools
  2. Spring Boot Actuator
  3. Spring Expression Language
  4. Spring AOP
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more