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?
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'
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.
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
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");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.