I'm always excited to take on new projects and collaborate with innovative minds.

Email

contact@niteshsynergy.com

Website

https://www.niteshsynergy.com/

@RequestParam vs @QueryParam vs @PathVariable vs @PathParam

@RequestParam vs @QueryParam vs @PathVariable vs @PathParam

Here's a comparison of the four annotations commonly used in Spring and JAX-RS (Java API for RESTful Web Services) for extracting parameters from a request:

  1. @RequestParam (Spring Framework):

    • Used to extract query parameters or form parameters from the request URL in Spring MVC or Spring Boot.
    • Typically used for GET requests with URL parameters or form submissions.

      image-148.png

      2. @QueryParam (JAX-RS):

    • Similar to @RequestParam in Spring but used in JAX-RS (used in frameworks like Jersey or RESTEasy).
    • Extracts query parameters from the URL, typically used with HTTP GET.
    image-149.png

    URL: /employees?department=HR

    3. @PathVariable (Spring Framework):

    Used to extract variables from the URI path, typically used with RESTful endpoints where the parameter is part of the URL path.

    Commonly used with dynamic URIs like /employees/{id}.

    image-150.png

    4. @PathParam (JAX-RS):

    Similar to @PathVariable in Spring but used in JAX-RS.

    Extracts parameters from the URI path.

    image-151.png

     

    image-153.png

     

  2. Feature@RequestParam (Spring)@QueryParam (JAX-RS)@PathVariable (Spring)@PathParam (JAX-RS)
    FrameworkSpring MVC / Spring BootJAX-RS (Jersey, RESTEasy, etc.)Spring MVC / Spring BootJAX-RS (Jersey, RESTEasy, etc.)
    UsageUsed to extract query parameters or form data from the URL or bodyExtracts query parameters from the URLExtracts values from the path of the URLExtracts values from the path of the URL
    HTTP MethodsTypically used in GET, POST, PUT, DELETE (for query/form data)Typically used in GET, POST, PUT, DELETE (for query data)Typically used in GET, POST, PUT, DELETE (for dynamic path segments)Typically used in GET, POST, PUT, DELETE (for dynamic path segments)
    Type of ParameterQuery parameters or form parametersQuery parametersPath parameters (part of the URL)Path parameters (part of the URL)
    Example URL/employees?department=HR/employees?department=HR/employees/123/employees/123
    Method Examplepublic List<Employee> getEmployees(@RequestParam String department)public List<Employee> getEmployees(@QueryParam("department") String department)public Employee getEmployeeById(@PathVariable Long id)public Employee getEmployeeById(@PathParam("id") Long id)
    Annotation PlacementCan be used with method parameters in controller classesCan be used with method parameters in JAX-RS resource classesCan be used with method parameters in controller classesCan be used with method parameters in JAX-RS resource classes
    Default Value SupportSupports default values using defaultValue attributeSupports default values using defaultValue attributeDoes not support default valuesDoes not support default values
    Required/OptionalDefault is required, but can be made optional using required = falseDefault is required, but can be made optional using defaultValueAlways required if the path is dynamicAlways required if the path is dynamic
    Common Use CaseFor handling form data or query parameters like search filters or paginationFor handling query parameters in RESTful web servicesFor handling resource identifiers in RESTful web servicesFor handling resource identifiers in RESTful web services
  3. Summary of Use Cases:

    @RequestParam / @QueryParam: Extract query parameters, often for filters, search criteria, or form data (e.g., ?page=1&size=10).

    @PathVariable / @PathParam: Extract path parameters, commonly used for dynamic resource identifiers (e.g., /employees/{id}).

     

3 min read
Nov 17, 2024
By Nitesh Synergy
Share

Leave a comment

Your email address will not be published. Required fields are marked *