📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
@PathVariable Annotation
- name - name of the path variable to bind to
- required - tells whether the path variable is required
- value - alias for name
@GetMapping(path = "/hello-world/{id}/{name}")
public HelloWorldBean helloWorldPathVariable(@PathVariable long id,
@PathVariable(name = "name") String name) {
return new HelloWorldBean(id, name);
}
Spring Boot @PathVariable Annotation Example
Development Steps
- Create a Spring Boot Application
- Project Structure
- Pom Dependencies
- Java Bean - HelloWorldBean.java
- Create REST Controller - HelloWorldController.java
- Run Application - Application.java
- Testing
1. Create a Spring Boot Application
>> Create Spring Boot Project in Spring Tool Suite [STS]
2. Project Structure
3. Pom Dependencies
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>net.javaguides.springboot</groupId>
<artifactId>springboot-helloworld-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-helloworld-app</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4. Java Bean - HelloWorldBean.java
package net.javaguides.springboot;
public class HelloWorldBean {
private long id;
private String message;
public HelloWorldBean(long id, String message) {
super();
this.id = id;
this.message = message;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
5. Create REST Controller - HelloWorldController.java
package net.javaguides.springboot;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@GetMapping(path = "/hello-world/{id}/{name}")
public HelloWorldBean helloWorldPathVariable(@PathVariable long id,
@PathVariable(name = "name") String name) {
return new HelloWorldBean(id, name);
}
}
@GetMapping(path = "/hello-world/{id}/{name}")
public HelloWorldBean helloWorldPathVariable(@PathVariable long id,
@PathVariable(name = "name") String name) {
return new HelloWorldBean(id, name);
}
6. Run Application - Application.java
mvn spring-boot:run
Comments
Post a Comment
Leave Comment