• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

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

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 422
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 162
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 422
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic