Home  >  Article  >  Java  >  How to Prevent @PathVariable Truncation in Spring MVC?

How to Prevent @PathVariable Truncation in Spring MVC?

Linda Hamilton
Linda HamiltonOriginal
2024-10-29 20:42:30306browse

How to Prevent @PathVariable Truncation in Spring MVC?

Resolving @PathVariable Truncation in Spring MVC

Problem:
When a Spring MVC controller defines a @PathVariable with a custom path variable argument, path segments containing special characters get truncated.

Example:

@RequestMapping(method = RequestMethod.GET, value = Routes.BLAH_GET + "/{blahName}")

Passing a URI with a path variable blah2010.08.19-02:25:47 would result in the blahName parameter being truncated to blah2010.08.

Resolution:
To prevent truncation, use a regular expression in the @RequestMapping argument:

@RequestMapping(method = RequestMethod.GET, value = Routes.BLAH_GET + "/{blahName:.+}")

The . = regex allows any number of characters (including specials) after the leading dot, effectively capturing the entire path segment as the path variable value. The request.getRequestURI() will then return the complete URI, including the full blahName.

The above is the detailed content of How to Prevent @PathVariable Truncation in Spring MVC?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn