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

How to Prevent @PathVariable Truncation in Spring MVC?

Linda Hamilton
Release: 2024-10-29 20:42:30
Original
409 people have browsed it

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}")
Copy after login

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:.+}")
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template