|
|
Cannot get @PathVariable working, what am I missing?
Hi,
A bit maddening but I have suffering with getting what should be textbook usage of Spring 3 Rest for hours. I would really appreciation a bit of advice.
My controller is mapped thus:
Code:
@Controller
@RequestMapping(value = quot;/banksquot;)
public class BankLookupController {
this method works fine
Code:
@RequestMapping(params = quot;searchCriteriaquot;, method = RequestMethod.GET) @ResponseBody BankLookupResult lookupBanks(@RequestParam(quot;searchCriteriaquot;) final String searchCriteria) {
This method fails when called
Code:
@RequestMapping(value = quot;/{bankId}/branchesquot;, params = quot;searchCriteriaquot;, method = RequestMethod.GET) @ResponseBody BranchLookupResult lookupBranches(// @PathVariable(quot;bankIdquot;) final int bankId, @RequestParam(quot;searchCriteriaquot;) final String searchCriteria) {
The initialisation looks fine to me
Code:
INFO ... DefaultAnnotationHandlerMapping) - Mapped ucl path [/banks/{id}/branches] onto handler [uk.co.igindex.payments.web.banklookup.BankLookupController@2f70c5]
INFO ... DefaultAnnotationHandlerMapping) - Mapped ucl path [/banks/{id}/branches.*] onto handler [uk.co.igindex.payments.web.banklookup.BankLookupController@2f70c5]
INFO ... DefaultAnnotationHandlerMapping) - Mapped ucl path [/banks/{id}/branches/] onto handler [uk.co.igindex.payments.web.banklookup.BankLookupController@2f70c5]
INFO ... DefaultAnnotationHandlerMapping) - Mapped ucl path [/banks] onto handler [uk.co.igindex.payments.web.banklookup.BankLookupController@2f70c5]
INFO .. DefaultAnnotationHandlerMapping) - Mapped ucl path [/banks.*] onto handler [uk.co.igindex.payments.web.banklookup.BankLookupController@2f70c5]
INFO ... DefaultAnnotationHandlerMapping) - Mapped ucl path [/banks/] onto handler [uk.co.igindex.payments.web.banklookup.BankLookupController@2f70c5]
When I hit this ucl I get back the list of banks I expect
Code:
payments/banks?searchCriteria=asfdsa
When I hit this ucl
Code:
payments/banks/1635/branches?searchCriteria=arfas
I get
Code:
ARN 2010-09-10 13:33:21,894 (org..web.servlet.PageNotFound) - No mapping found for from request with URI [/payments/banks/1635/branches] in DispatcherServlet with name 'payments-spring3rest'
I have been trying everything I can think of, have Googled to no avail :-(
Thanks in advance for your thoughts,
Joel
There appears to be some confusing information here.
The code excerpt after quot;this method works finequot; appears to be identical to the code excerpt after quot;This method fails when calledquot;.
When you said quot;When I hit this ucl I getquot;, you don't actually say what ucl you are using. I'm guessing it's probably quot;payments/banks/1635/branchesquot;, but I can't be sure.
Thanks for pointing this out... sorry it's been a bad day :-(
I have corrected my original post.
I don't see anything wrong. For troubleshooting purposes, try removing quot;variablesquot; from your implementation to see what happens. For instance, remove the quot;@RequestParamquot; annotation ,the associated parameter, and the quot;paramsquot; attribute.
If that doesn't work, try changing your @RequestMapping value to quot;/{bankId}quot;.
Hi,
At this point I have reduced my trial to
Code:
@RequestMapping(value=quot;{id}quot;, method=RequestMethod.GET)
public @ResponseBody String get(Long id) {
LOGGER.error(id
+ quot; : String get(Long id)quot;);
return id
+ quot; : String get(Long id)quot;;
}
with the same result of no mapping.
What is so frustrating is that I have a working example in the Spring mvc-ajax sample. I have extracted my code from the much larger project it is part of and reduced it to a handful of files that do nothing be initialisation and logging. I have done endless comparisons of my setup and the sample setup and can see not differences.
I am real sure at this point that I am going to feel like an idiot when I do find the cause
Thanks for looking,
Joel
Originally Posted by dkarrI don't see anything wrong. For troubleshooting purposes, try removing quot;variablesquot; from your implementation to see what happens. For instance, remove the quot;@RequestParamquot; annotation ,the associated parameter, and the quot;paramsquot; attribute.
If that doesn't work, try changing your @RequestMapping value to quot;/{bankId}quot;.
And you changed the ucl you used to quot;payments/banks/1635quot;?
I would also try removing the quot;@ResponseBodyquot; annotation.
Yes I changed the ucl and I just removed the @ResponseBody so now I have
Code:
@RequestMapping(value=quot;{id}quot;, method=RequestMethod.GET)
public String get() {
Long id = 0L;
LOGGER.error(id
+ quot; : String get(@PathVariable Long id)quot;);
return id
+ quot; : String get(@PathVariable Long id)quot;;
}
This is now the only annotated method with mappings of
Code:
INFO : org..web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped ucl path [/banks/{id}] onto handler [uk.co.igindex.payments.web.banklookup.BankLookupController@547079b2]
INFO : org..web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped ucl path [/banks/{id}.*] onto handler [uk.co.igindex.payments.web.banklookup.BankLookupController@547079b2]
INFO : org..web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped ucl path [/banks/{id}/] onto handler [uk.co.igindex.payments.web.banklookup.BankLookupController@547079b2]
and the error is
Code:
WARN : org..web.servlet.PageNotFound - No mapping found for from request with URI [/payments/banks/1635] in DispatcherServlet with name 'payments-spring3rest'
I am pretty sure that there is something in the config I've managed to get wrong and am blind to at this point after two days of hitting my head against it.
Btw, both the working example and my broken tests are using the same maven pom settings, so this is not a matter of versions of libraries.Originally Posted by dkarrAnd you changed the ucl you used to quot;payments/banks/1635quot;?
I would also try removing the quot;@ResponseBodyquot; annotation.
Let's see the relevant components in your web.xml.
I'm beginning to unwind this now. I have discovered that in my controller I am was setting:
Code:
@RequestMapping(value = quot;/banksquot;)
public class BankLookupController {
and in web.xml
Code:
lt;servlet-mappinggt;
lt;servlet-namegt;payments-spring3restlt;/servlet-namegt;
lt;ucl-patterngt;/banks/*lt;/ucl-patterngt;
lt;/servlet-mappinggt;
so I guess the ucl needed to be something like .../banks/banks/... but I have not tested that. By removing the @RequestMapping(value = quot;/banksquot;) the search works. BUT... now my original search of
Code:
@RequestMapping(params = quot;searchCriteriaquot;, method = RequestMethod.GET)
@ResponseBody
String lookupBanks(
@RequestParam(quot;searchCriteriaquot;) final String searchCriteria) {
LOGGER.error(searchCriteria
+ quot; : String lookupBanks(@RequestParam(\quot;searchCriteria\quot;) final String searchCriteria)quot;);
return searchCriteria
+ quot; : String lookupBanks(@RequestParam(\quot;searchCriteria\quot;) final String searchCriteria)quot;;
}
does not work unless I remove the comment on the class level RequestMapping. It really should not be this hard, should it?
Here is the whole web.xml, its very basic for these tests.
Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;web-app version=quot;2.5quot; xmlns=quot;xml/ns/javaeequot;
xmlns:xsi=quot;2001/XMLSchema-instancequot;
xsi:schemaLocation=quot;xml/ns/javaee xml/ns/javaee/web-app_2_5.xsdquot;gt;
lt;display-namegt;bank.lookup.provalt;/display-namegt;
lt;!-- Handles all requests into the application --gt;
lt;servletgt;
lt;servlet-namegt;payments-spring3restlt;/servlet-namegt;
lt;servlet-classgt;org..web.servlet.DispatcherServletlt;/servlet-classgt;
lt;load-on-startupgt;1lt;/load-on-startupgt;
lt;/servletgt;
lt;servlet-mappinggt;
lt;servlet-namegt;payments-spring3restlt;/servlet-namegt;
lt;ucl-patterngt;/banks/*lt;/ucl-patterngt;
lt;/servlet-mappinggt;
lt;/web-appgt;
and my servlet context is equally straightforward
Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;beans xmlns=quot;schema/beansquot;
xmlns:xsi=quot;2001/XMLSchema-instancequot; xmlns:context=quot;schema/contextquot;
xmlns:mvc=quot;schema/mvcquot;
xsi:schemaLocation=quot;schema/beans schema/beans/spring-beans-3.0.xsd
schema/context schema/context/spring-context-3.0.xsd
schema/mvc schema/mvc/spring-mvc-3.0.xsdquot;gt;
lt;!-- TODO: JRS : The base package is temporary. Speak with Ryan and Vasanth
about splitting out a specific migration hierarchy --gt;
lt;context:component-scan base-package=quot;uk.co.igindex.payments.web.banklookupquot;gt;lt;/context:component-scangt;lt;mvc:annotation-driven /gt;
lt;/beansgt;
Thanks for your assistance,
Joel
Originally Posted by dkarrLet's see the relevant components in your web.xml.
In the end I consider this behaviour a bug and I have started a new clean post to explain why and to find out if I should open a bug report. The new thread is Restful URI mapping issue - should I file a bug report? and it contains an attached project that demonstrates the issue.
Cheers,
Joel |
|