Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Victor Fernandez
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 363
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Kuladip Yadav
Ranch Hand
Posts: 162
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 363
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I was looking for a difference between the @RequestParam and @Pathvariable and this thread was really useful understanding the usage of both.
 
Zeeshan Akhter
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic