@RequestParam and @PathVariable
@RequestParam and @PathVariable annotations are used for accessing the values from the request. Both the the annotations have the significant purpose and use respectively. The key difference between @RequestParam and @PathVariable is that @RequestParam used for accessing the values of the query parameters where as @PathVariable used for accessing the values from the URI template.
In this tutorial I will explain the @RequestParam and @PathVariable annotations used in Spring MVC with a simple example and syntax used for accessing the values from query string and URI template.
@RequestParam vs @PathVariable Tutorial
@RequestParam vs @PathVariable Tutorial
@RequestParam
@RequestParam annotation used for accessing the query parameter values from the request. Look at the following request URL:
http://localhost:8080/springmvc/hello/101?param1=10¶m2=20
In the above URL request, the values for param1 and param2 can be accessed as below:
The following are the list of parameters supported by the @RequestParam annotation:
defaultValue – This is the default value as a fallback mechanism if the request is not having the value or it is empty.
name – Name of the parameter to bind
required – Whether the parameter is mandatory or not. If it is true, failing to send that parameter will fail.
value – This is an alias for the name attribute
@PathVariable
@PathVariable identifies the pattern that is used in the URI for the incoming request. Let’s look at the below request URL:
http://localhost:8080/springmvc/hello/101?param1=10¶m2=20
The above URL request can be written in your Spring MVC as below:
The @PathVariable annotation has only one attribute value for binding the request URI template. It is allowed to use the multiple @PathVariable annotation in the single method. But, ensure that no more than one method has the same pattern.
While developing RESTful Web Services, it will be useful to use this annotation for forming the more flexible URI (Also Read : REST API Best Practices).
@PathParam vs @QueryParam
If you want to understand the difference between @PathParam and @QueryParam, the above two annotations are defined in JAX-RS implementations for the exact purpose of @PathVariable and @RequestParam which is defined in spring’s REST implementations. But, you could use either of the annotations without any difference in the behavior.
RequestParam vs PathVariable Example
Difference Between @RequestParam and @PathVariable in Spring MVC
RequestParam vs PathVariable
Here is the complete source code for the example that demonstrates the key difference between RequestParam and PathVariable annotations. I have used Spring Boot for writing this example application. If you feel I have missed any key points about the RequestParam and PathVariable, please write it in the comments section.
SpringMVCController.java
Application.java
WebMvcConfig.java
server.contextPath=/springmvc
pom.xml
@RequestParam and @PathVariable annotations are used for accessing the values from the request. Both the the annotations have the significant purpose and use respectively. The key difference between @RequestParam and @PathVariable is that @RequestParam used for accessing the values of the query parameters where as @PathVariable used for accessing the values from the URI template.
In this tutorial I will explain the @RequestParam and @PathVariable annotations used in Spring MVC with a simple example and syntax used for accessing the values from query string and URI template.
@RequestParam vs @PathVariable Tutorial
@RequestParam vs @PathVariable Tutorial
@RequestParam
@RequestParam annotation used for accessing the query parameter values from the request. Look at the following request URL:
http://localhost:8080/springmvc/hello/101?param1=10¶m2=20
In the above URL request, the values for param1 and param2 can be accessed as below:
public String getDetails( @RequestParam(value="param1", required=true) String param1, @RequestParam(value="param2", required=false) String param2){ ... }
The following are the list of parameters supported by the @RequestParam annotation:
defaultValue – This is the default value as a fallback mechanism if the request is not having the value or it is empty.
name – Name of the parameter to bind
required – Whether the parameter is mandatory or not. If it is true, failing to send that parameter will fail.
value – This is an alias for the name attribute
@PathVariable
@PathVariable identifies the pattern that is used in the URI for the incoming request. Let’s look at the below request URL:
http://localhost:8080/springmvc/hello/101?param1=10¶m2=20
The above URL request can be written in your Spring MVC as below:
@RequestMapping("/hello/{id}") public String getDetails(@PathVariable(value="id") String id, @RequestParam(value="param1", required=true) String param1, @RequestParam(value="param2", required=false) String param2){ ....... }
The @PathVariable annotation has only one attribute value for binding the request URI template. It is allowed to use the multiple @PathVariable annotation in the single method. But, ensure that no more than one method has the same pattern.
While developing RESTful Web Services, it will be useful to use this annotation for forming the more flexible URI (Also Read : REST API Best Practices).
@PathParam vs @QueryParam
If you want to understand the difference between @PathParam and @QueryParam, the above two annotations are defined in JAX-RS implementations for the exact purpose of @PathVariable and @RequestParam which is defined in spring’s REST implementations. But, you could use either of the annotations without any difference in the behavior.
RequestParam vs PathVariable Example
Difference Between @RequestParam and @PathVariable in Spring MVC
RequestParam vs PathVariable
Here is the complete source code for the example that demonstrates the key difference between RequestParam and PathVariable annotations. I have used Spring Boot for writing this example application. If you feel I have missed any key points about the RequestParam and PathVariable, please write it in the comments section.
SpringMVCController.java
package net.aid.spring.boot.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class SpringMVCController { @RequestMapping("/hello/{id}") public String getDetails(@PathVariable(value="id") String id, @RequestParam(value="param1", required=true) String param1, @RequestParam(value="param2", required=false) String param2){ System.out.println("ID : " + id); System.out.println("Param 1 : " + param1); System.out.println("Param 2 : " + param2); return "hello"; } }
Application.java
package net.aid.spring.boot.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @SpringBootApplication public class Application extends WebMvcConfigurerAdapter{ public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
WebMvcConfig.java
package net.aid.spring.boot.example.controller; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation. DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @EnableWebMvc public class WebMvcConfig extends WebMvcConfigurerAdapter{ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("WEB-INF/"); resolver.setSuffix(".jsp"); return resolver; } }application.properties
server.contextPath=/springmvc
pom.xml
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.aid</groupId> <artifactId>boot</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>boot</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <finalName>boot</finalName> </build> </project>