• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

@PathVariable – URI Template Patterns in Spring MVC

October 13, 2013 //  by Krishna Srinivasan//  Leave a Comment

This tutorial explains how to customize / template pattern on  request URI for Spring MVC web application. @RequestMapping annotation used for defining the request URL for the class level and method levels. It is generic annotation where developer can configure the relative paths inside application context. However, it doesn’t offer flexible URI patterns on its own. We have to use @PathVariable for accepting the customized or more dynamic parameters in the request paths. You can configure the complete path inside @RequestMapping with the placeholders for the dynamic URIs.

Look at the below code example. I have posted only the controller class with the @PathVariable configuration. If you are looking for the complete example for Spring MVC application, then read example 1 and example 2. If you have any questions, please write it in the comments section.

package javabeat.net;
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.RequestMethod;

@Controller
public class TestController {
     @RequestMapping(value="/user/{userId}/roles/{roleId}",method = RequestMethod.GET)
     public String getLogin(@PathVariable("userId") String userId,
         @PathVariable("roleId") String roleId){
         System.out.println("User Id : " + userId);
         System.out.println("Role Id : " + roleId);
         return "hello";
     }
     @RequestMapping(value="/product/{productId}",method = RequestMethod.GET)
     public String getProduct(@PathVariable("productId") String productId){
           System.out.println("Product Id : " + productId);
           return "hello";
     }
     @RequestMapping(value="/javabeat/{regexp1:[a-z-]+}",
           method = RequestMethod.GET)
     public String getRegExp(@PathVariable("regexp1") String regexp1){
           System.out.println("URI Part 1 : " + regexp1);
           return "hello";
     }
}

  • @PathVariable is very useful for dynamic URIs.
  • There is no limit on the number of parameters used in a single method. You can use more than one dynamic parameter in a single method’s parameter.
  • This can be used with Map argument. Parameters will be populated to the Map object.
  • A @PathVariable argument can be of any simple type such as int, long, Date, etc.

Hope this helps to understand the use of PathVariable in your controller. If you find any issues, please write your comments. I will help you in understanding this example.

Category: Spring FrameworkTag: Spring Framework, Spring MVC

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How To Build Spring MVC Web Application With Maven?
Next Post: Creating JSON Data Using The Java JSON API (JSR 353) »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact