2

I have a controller that works fine for other request mappings, but I'm having trouble getting one to work with the @PathVariable annotation.

In this example (see "URI Template Patterns") it has this example:

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
    Owner owner = ownerService.findOwner(ownerId);
    model.addAttribute("owner", owner);
    return "displayOwner";
}

My code looks like this:

@RequestMapping( value="/ViewReport/json", method = RequestMethod.GET)
public ModelAndView TestJson(final Model model){
    model.addAttribute("fnord", "{\"hello\":\"awesome\"}"); 
    return new ModelAndView("reportViewJson");  
}    

@RequestMapping( value="/ViewReport/jayson/{foobar}", method = RequestMethod.GET)
public ModelAndView TestJayz(final Model model, @PathVariable String foobar){
    model.addAttribute("fnord", "{\"hello\":\""+foobar+"\"}");  
    return new ModelAndView("reportViewJson");  
} 

Then I have a web.xml file containing this:

  <servlet-mapping>
    <servlet-name>autoreport</servlet-name>
    <url-pattern>/ViewReport/jayson/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>autoreport</servlet-name>
    <url-pattern>/ViewReport/json</url-pattern>
  </servlet-mapping>

Now, when I navigate to /ViewReport/json, I get the JSON {"hello": "awesome"} as I would expect. However, when I navigate to /ViewReport/jayson/42 I get

HTTP Status 404 -

type Status report

message

description The requested resource () is not available.

Apache Tomcat/7.0.29

What am I doing wrong with my usage of the PathVariable, and what do I need to do to fix it?

Update

When my server starts up it spits out these lines:

[ INFO] 14:15(AbstractUrlHandlerMapping.java:registerHandler:315)
Mapped URL path [/ViewReport/json.*] onto handler 'reportViewController'

[DEBUG] 14:15(AbstractBeanFactory.java:doGetBean:246)
Returning cached instance of singleton bean 'reportViewController'

[ INFO] 14:15(AbstractUrlHandlerMapping.java:registerHandler:315)
Mapped URL path [/ViewReport/json/] onto handler 'reportViewController'

[DEBUG] 14:15(AbstractBeanFactory.java:doGetBean:246)
Returning cached instance of singleton bean 'reportViewController'

[ INFO] 14:15(AbstractUrlHandlerMapping.java:registerHandler:315)
Mapped URL path [/ViewReport/jayson/{foobar}] onto handler 'reportViewController'

And when I make the request I get this:

[DEBUG] 20:45(DispatcherServlet.java:doService:823)
DispatcherServlet with name 'autoreport' processing GET request for [/ViewReport/jayson/fnord]

[ WARN] 20:45(DispatcherServlet.java:noHandlerFound:1108)
No mapping found for HTTP request with URI [/ViewReport/jayson/fnord] in DispatcherServlet with name 'autoreport'

3 Answers 3

2

Spring has magic; parameter order matters. Try inverting the order of your handler parameters. Instead of this:

public ModelAndView TestJayz(final Model model, @PathVariable String foobar)

Try this:

public ModelAndView TestJayz(@PathVariable String foobar, final Model model)

Take 2:

Try changing the mapping in your web.xml to something more generic. For example:

<servlet-mapping>
    <servlet-name>autoreport</servlet-name>
    <url-pattern>/ViewReport/*</url-pattern>
</servlet-mapping>

I used /spring/* in my test web.xml, @RequestMapping("/blammy/{value}") in my controller, then sent the request to "{WEBROOT}/spring/blammy/testvalue" where "{WEBROOT}" was the web-root of my test app.

2
  • 1
    The only parameter order that matter in Spring is with BindingResult and there's a very good reason. Why would it matter here? Dec 13, 2013 at 21:14
  • And sadly this didn't work. But I've added some more info - apparently it is mapping the path, but somehow it's not being directed correctly... Dec 13, 2013 at 21:21
0

request mappings a relative to the dispatcher servlet mapping (by default, see alwaysUseFullPath property in DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter for more information).

if you request a resource /ViewReport/jayson/fnord spring mvc is looking for a handler mapped to fnord.

you would need to request /ViewReport/jayson/ViewReport/jayson/fnord

0

Have you tried @PathVariable(value="foobar") String foobar ?

@RequestMapping( value="/ViewReport/jayson/{foobar}", method = RequestMethod.GET)
public ModelAndView TestJayz(final Model model, @PathVariable(value="foobar") String foobar){
    model.addAttribute("fnord", "{\"hello\":\""+foobar+"\"}");  
    return new ModelAndView("reportViewJson");  
} 
2
  • I'll double check, but I'm fairly certain that I did. Dec 16, 2013 at 5:16
  • Yep, and still no dice. Dec 16, 2013 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.