From the course: Complete Guide to Spring MVC

Mapping requests: URI patterns - Spring Tutorial

From the course: Complete Guide to Spring MVC

Mapping requests: URI patterns

- [Instructor] Mapping Requests: URI Patterns. In Spring MVC, URI patterns are used to match incoming HTTP requests to specific controller methods. They provide a flexible way to define URL structures and extract information from the request path. In your Hello World app, you're currently using a simple URI pattern. Can you identify where that pattern is? If you guessed greeting, that is correct. This means that your application can handle get requests to the greeting URL. To further that example, I provided this snippet of code here. In this example, the greeting and name pattern allows you to include a dynamic name in the URL. This makes your application more flexible and allows users to customize the greeting message. So you can use more complex URI patterns to match different types of requests. So for example, you could even extend a pattern to look something like users, user ID, order, order ID to match requests for specific user orders. You could use regular expressions with the URI pattern to validate input data, and you can also use path variables to extract information from the URL and use it in your controller methods. There are four key URI pattern concepts that are very helpful to know. Let's start with PathPattern. This is a pre-parsed pattern that efficiently matches URL paths. It is the recommended solution for web applications and is enabled by default starting after Spring MVC 5.3. So for example, if I just used this where it's user, user ID, order, order ID, this pattern would match URLs like user. Let's say my ID is 123, order, and then my order number is 456. The user ID and order ID placeholders capture the user ID and order ID from the URL. Next would be AntPathMatcher. This is a more general purpose pattern matcher that can be used for other scenarios that might be less efficient for web applications. So for example, a generic resources with two double asterisks on the side. This pattern matches any URL that starts with resources and has any number of additional path segments. AntPathMatcher is less efficient with path pattern for web applications, but it can be useful for other scenarios like matching resource paths in the class path. The third key pattern concept is URI variables. Placeholders within URL patterns that capture values from the request path, like the example we used, greeting/{name}. The name placeholder captures the value from the URL path and makes it available as a variable in the controller method. Finally, number four would be suffix matching. This is where you have the ability to match requests based on file extensions. This feature was disabled by default from Spring MVC 5.3 to avoid security vulnerabilities. So you would need to manually turn it back on if you want to use it. So think person.json is a specific file. The suffix matching allows you to match requests based on the file extension, and then you could also use this within content negotiation. However, this is discouraged, again due to security vulnerabilities. So when looking at pattern syntax, here are some aspects that you need to know. So for using the forward slash, use this to separate path segments. And for wild cards, we saw this in the AntPathMatcher, you'd use an asterisk to match zero or more characters within a path segment, and then you would use a double asterisk to match zero or more path segments. And then for URI variables, you would capture a path segment as a variable if you use forward slash variable name or varName. And then second for variable name regex, this is where you'll capture a path segment and validate it against a regular expression. And finally, for embedded placeholders where you have the dollar sign dot, dot, dot, this resolves placeholders with values from property sources. Here are some examples of URI patterns that you can use and what the matched URL would look like. Some best practices here are to use clear and concise URI patterns that reflect your application structure. Avoid ambiguity and avoid using excessive wild cards or randomness in the patterns. Again, you want this to be modular and scale, especially if you're using this within enterprise applications. But if you're just working on your own side projects, be as ambiguous and excessive as you'd like. Consider using path variables to make your URLs more dynamic and flexible. And then be mindful of security applications when using URI variables and path extensions. So for example, if you want a file to be publicly available, sure, go ahead and have the file name in the URL, but if there needs to be more security around it, then I wouldn't necessarily use the suffix matching as an option. So be effective when you're using URI patterns in Spring MVC so that you can create well-structured and user friendly web applications.

Contents