Spring MVC PathVariable truncate issue

In earlier posts we have seen How Spring MVC Path Variable work.
There is one issue with the @PathVariable. The last @PathVariable in the URL truncates the value passed after the . (dot).
In this post we will see the ways to fix the Spring MVC PathVariable truncate issue.

Spring MVC PathVariable Truncate Issue

We have a Controller method as shown below

When we use the URL – http://localhost:8080/sampleproject/employee/100/kscodes@gmail.com we expect to get the output as

But instead we get the output as

As you may notice the part after .(dot) for the email is truncated by the @PathVariable.

We can fix this issue by 2 ways.

1. Using Regex

Use a regex :.+ and add it in the last PathVariable on whose values get truncated.

Now using http://localhost:8080/sampleproject/employee/100/kscodes@gmail.com will give us the complete value of email.

2. Using a slash at the end

Another way to handle the issue is to add a slash at the end of the URL.

The only care you need to take is you will need to add the slash every request you make to the method.
http://localhost:8080/sampleproject/employee/100/kscodes@gmail.com/ , else you may get a 404 error.

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn