aspose file tools*
The moose likes Spring and the fly likes How to match a Spring @RequestMapping having a @pathVariable containing “/” ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to match a Spring @RequestMapping having a @pathVariable containing “/” ?" Watch "How to match a Spring @RequestMapping having a @pathVariable containing “/” ?" New topic
Author

How to match a Spring @RequestMapping having a @pathVariable containing “/” ?

Victor Fernandez
Greenhorn

Joined: Mar 20, 2009
Posts: 26
Hi

I am doing the following request from the client:



where the search term "hello/there" has been URLencoded.

On the server I am trying to match this URL using the following request mapping:


But I am getting error 404 on the server, due I don't have any match for the URL. I noticed that the URL is decoded before Spring gets it. Therefore is trying to match /search/hello/there which does not have any match.

I found a Jira related to this problem here: http://jira.springframework.org/browse/SPR-6780 .But I still don't know how to solve my problem.

Any ideas?

Thanks
Craig Walls
author
Ranch Hand

Joined: Sep 19, 2003
Posts: 301

Honestly...I have no idea how to do that. But at the same time, I have no idea why you'd want to. I'm not saying that there's not a valid reason..but I'd be interested in knowing what that reason is. Help me understand.


Spring in Action - Unleash POJO power in your applications!
Modular Java - Discover the secret weapon to modularity on the Java platform!
XDoclet in Action - Your complete guide to code generation with XDoclet.
Kuladip Yadav
Ranch Hand

Joined: Jul 30, 2008
Posts: 162

Hi Victor,

If you use searchTerm as request parameter then you can achieve it as fallows :


So your can change you request as


And controller


Thanks.
Victor Fernandez
Greenhorn

Joined: Mar 20, 2009
Posts: 26
Craig Walls wrote:
Honestly...I have no idea how to do that. But at the same time, I have no idea why you'd want to. I'm not saying that there's not a valid reason..but I'd be interested in knowing what that reason is. Help me understand.


Let me try to explain .

I am using REST Urls in the controller. But there is the possibility that the search term contains the "/" character. And I thought that encoding the URL should be enough for the server to match the url, and after that, URLdecode the searchterm in the appropriate method. But the problem as I described in my previous post is that the URL decoding occurs before the URL matching.

I found two solutions but none of them are optimal for me:
- use a "special" character to replace the "/" in the client and later on, do the opposite in the server
- do not use REST URLs

Any new ideas on this?

Thanks
Victor Fernandez
Greenhorn

Joined: Mar 20, 2009
Posts: 26
Kuldeep Yadav wrote:Hi Victor,

If you use searchTerm as request parameter then you can achieve it as fallows :


So your can change you request as


And controller


Thanks.


Hi Kuldeep Yadav!, thanks for your reply. Your solution works but I still want to keep REST Urls.
Craig Walls
author
Ranch Hand

Joined: Sep 19, 2003
Posts: 301
This is a case where I should say that REST is great and should be strived for...but it doesn't fit in all cases. It's not a silver bullet.

Ultimately, a REST URL should be a reference to some resource or collection of resources. It could be argued that search results are a collection of resources and thus a search URL should be RESTful. But this is a case where I'd fall back on letting search not be quite so RESTful...just my opinion, though.

Certainly, however, the each of the items in the search results are probably resources and thus should have URLs that are RESTful.
Victor Fernandez
Greenhorn

Joined: Mar 20, 2009
Posts: 26
Craig Walls wrote:This is a case where I should say that REST is great and should be strived for...but it doesn't fit in all cases. It's not a silver bullet.

Ultimately, a REST URL should be a reference to some resource or collection of resources. It could be argued that search results are a collection of resources and thus a search URL should be RESTful. But this is a case where I'd fall back on letting search not be quite so RESTful...just my opinion, though.

Certainly, however, the each of the items in the search results are probably resources and thus should have URLs that are RESTful.


Yes maybe you're right. And this is one of the cases when REST URLs don't fit too well.

Thanks
Akhash Ramamurthy
Greenhorn

Joined: Nov 02, 2009
Posts: 25
Thanks guys. I was looking for a difference between the @RequestParam and @Pathvariable and this thread was really useful understanding the usage of both.


Software Engg
Akhash
Zeeshan Akhter
Greenhorn

Joined: Feb 05, 2013
Posts: 1
That way of Request Param will work in mine Problem

@RequestMapping(value = "/ex/bars", params = { "id", "second" })
@ResponseBody
public String getBarBySimplePathWithExplicitRequestParams(@RequestParam("id") long id) {
return "Narrow Get a specific Bar with id=" + id;
}

Read more: http://latest-tutorial.com/2014/05/30/spring-6-ways-requestmapping/#ixzz33AxETORb
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2397
    
  28

Thanks Zeeshan and welcome to Coderanch,

Unfortunatly since, the thread is nearly 3 years old, this information might be too late for the original poster
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to match a Spring @RequestMapping having a @pathVariable containing “/” ?