avatarVinotech

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2373

Abstract

n>)</span> String name) { <span class="hljs-keyword">return</span> <span class="hljs-string">"User Name: "</span> + name; } }</pre></div><h1 id="8720">3. @PathParam (From JAX-RS, not used in Spring MVC)</h1><ul><li>Used in <b>JAX-RS</b> to extract values from the <b>path variable</b> in the URL.</li><li>Spring Boot does <b>not</b> use <code>@PathParam</code>, instead, <code>@PathVariable</code> should be used.</li></ul><h2 id="38fa">Example (JAX-RS, Not Spring Boot):</h2><div id="3a5f"><pre><span class="hljs-meta">@Path(<span class="hljs-string">"/users"</span>)</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserService</span> {

<span class="hljs-meta">@GET</span>
<span class="hljs-meta">@Path(<span class="hljs-string">"/{id}"</span>)</span>
<span class="hljs-meta">@Produces(MediaType.TEXT_PLAIN)</span>
<span class="hljs-keyword">public</span> String getUserById(<span class="hljs-meta">@PathParam(<span class="hljs-string">"id"</span>)</span> int id) {
    <span class="hljs-keyword">return</span> <span class="hljs-string">"User ID: "</span> + id;
}

}</pre></div><h1 id="f9c1">4. @PathVariable</h1><ul><li>Used in <b>Spring Boot</b> to extract values from the <b>URI path</b>.</li><li>Works with <b>RESTful endpoints</b>.</li></ul><div id="ed0e"><pre><span class="hljs-meta">@RestController</span> <span class="hljs-meta">@RequestMapping(<span class="hljs-string">"/api"</span>)</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserController</span> {

<span class="hljs-meta">@GetMapping(<span class="hljs-string">"/users/{id}"</span>)</span>
<span class="hljs-keyword">public</span> String getUserById(<span class="hljs-meta">@PathVariable(<span class="hljs-string">"id"</span>)</span> int userId) {
    <span class="hljs-keyword">return</span> <span class="hljs-string">"User ID: "</span> + userId;
}

}</pre></div><p id="344c"><b>Usage</b>:</p><div id="839d"><pre>GET http://localhost:8080/api/users/10</pre></div><p id="38b5"><b>Response:</b> <code>User ID: 10</code></p><h1 id="a076">Use Case Scenario (Combining @RequestParam and @PathVariable)</h1><p id="a9ce"><b>Scenario:</b> A user management system where:</p><ol><li>We fetch user details by ID (<code>

Options

@PathVariable</code>).</li><li>We apply an optional filter (<code>@RequestParam</code>) for active/inactive users.</li></ol><p id="cea4"><b>Example Code</b></p><div id="b551"><pre><span class="hljs-meta">@RestController</span> <span class="hljs-meta">@RequestMapping</span>(<span class="hljs-string">"/api"</span>) <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserController</span> {

<span class="hljs-meta">@GetMapping</span>(<span class="hljs-string">"/users/{id}"</span>)
<span class="hljs-keyword">public</span> <span class="hljs-title class_">String</span> <span class="hljs-title function_">getUserById</span>(<span class="hljs-params">
        <span class="hljs-meta">@PathVariable</span>(<span class="hljs-string">"id"</span>) int userId,
        <span class="hljs-meta">@RequestParam</span>(name = <span class="hljs-string">"active"</span>, required = <span class="hljs-literal">false</span>, defaultValue = <span class="hljs-string">"true"</span>) <span class="hljs-built_in">boolean</span> activeStatus</span>) {
    <span class="hljs-keyword">return</span> <span class="hljs-string">"User ID: "</span> + userId + <span class="hljs-string">", Active: "</span> + activeStatus;
}

}</pre></div><p id="d304"><b>Usage</b></p><div id="cf3d"><pre>GET http://localhost:8080/api/users/10?active=<span class="hljs-literal">false</span></pre></div><p id="04ba"><b>Response:</b> <code>User ID: 10, Active: false</code></p><p id="aaea"><b>Summary Table</b></p><figure id="8127"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*-PkIBWAxlzE0VGK1Vc9d9A.png"><figcaption></figcaption></figure><h1 id="c583">Key Takeaways</h1><ul><li><b>Use <code>@RequestParam</code></b> for <b>query parameters</b> in Spring Boot.</li><li><b>Use <code>@PathVariable</code></b> for <b>path parameters</b> in Spring Boot.</li><li><code><b>@QueryParam</b></code><b> and <code>@PathParam</code> are JAX-RS specific</b> and not used in Spring Boot.</li></ul><blockquote id="7cfa"><p><b>If you found my articles useful, please consider giving it many claps </b><i>👏👏👏</i> <b>and sharing it with your friends and colleagues. Please </b><i>🔖</i> <b>save it for future reference. </b><i>🔗</i> <b>Follow and </b><i>🔔</i><b>subscribe me for more updates</b></p></blockquote></article></body>

Understanding @RequestParam, @QueryParam, @PathParam, and @PathVariable in Spring Boot

1. @RequestParam

  • Used to extract query parameters from the request URL.
  • Supports optional and default values.
  • Commonly used in GET requests.
@RestController
@RequestMapping("/api")
public class UserController {

    @GetMapping("/users")
    public String getUserByName(@RequestParam(name = "name", required = false, defaultValue = "Guest") String name) {
        return "User Name: " + name;
    }
}

Usage:

GET http://localhost:8080/api/users?name=John

Response: User Name: John

If the parameter name is missing, it defaults to "Guest".

2. @QueryParam (From JAX-RS, not used in Spring MVC)

  • Works similarly to @RequestParam but is used in JAX-RS (Java EE REST API).
  • Spring Boot does not use @QueryParam, instead, @RequestParam should be used.

Example (JAX-RS, Not Spring Boot):

@Path("/users")
public class UserService {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getUserByName(@QueryParam("name") String name) {
        return "User Name: " + name;
    }
}

3. @PathParam (From JAX-RS, not used in Spring MVC)

  • Used in JAX-RS to extract values from the path variable in the URL.
  • Spring Boot does not use @PathParam, instead, @PathVariable should be used.

Example (JAX-RS, Not Spring Boot):

@Path("/users")
public class UserService {

    @GET
    @Path("/{id}")
    @Produces(MediaType.TEXT_PLAIN)
    public String getUserById(@PathParam("id") int id) {
        return "User ID: " + id;
    }
}

4. @PathVariable

  • Used in Spring Boot to extract values from the URI path.
  • Works with RESTful endpoints.
@RestController
@RequestMapping("/api")
public class UserController {

    @GetMapping("/users/{id}")
    public String getUserById(@PathVariable("id") int userId) {
        return "User ID: " + userId;
    }
}

Usage:

GET http://localhost:8080/api/users/10

Response: User ID: 10

Use Case Scenario (Combining @RequestParam and @PathVariable)

Scenario: A user management system where:

  1. We fetch user details by ID (@PathVariable).
  2. We apply an optional filter (@RequestParam) for active/inactive users.

Example Code

@RestController
@RequestMapping("/api")
public class UserController {

    @GetMapping("/users/{id}")
    public String getUserById(
            @PathVariable("id") int userId,
            @RequestParam(name = "active", required = false, defaultValue = "true") boolean activeStatus) {
        return "User ID: " + userId + ", Active: " + activeStatus;
    }
}

Usage

GET http://localhost:8080/api/users/10?active=false

Response: User ID: 10, Active: false

Summary Table

Key Takeaways

  • Use @RequestParam for query parameters in Spring Boot.
  • Use @PathVariable for path parameters in Spring Boot.
  • @QueryParam and @PathParam are JAX-RS specific and not used in Spring Boot.

If you found my articles useful, please consider giving it many claps 👏👏👏 and sharing it with your friends and colleagues. Please 🔖 save it for future reference. 🔗 Follow and 🔔subscribe me for more updates

Path Variable
Requestparam
Spring Boot
Spring Boot Annotation
Spring
Recommended from ReadMedium