如何在Java + Spring中正确使用@PathVariable?

时间:2013-12-13 20:51:17

标签: java spring spring-mvc

我有一个适用于其他请求映射的控制器,但是我无法使用@PathVariable注释。

this example中(参见“URI模板模式”),它有这个例子:

@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";
}

我的代码如下所示:

@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");  
} 

然后我有一个web.xml文件,其中包含:

  <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>

现在,当我导航到/ViewReport/json时,我会按照我的预期获得JSON {"hello": "awesome"}。但是,当我导航到/ViewReport/jayson/42时,我得到了

HTTP Status 404 -

type Status report

message

description The requested resource () is not available.

Apache Tomcat/7.0.29

我对PathVariable的使用有什么问题,我需要做些什么才能修复它?

更新

当我的服务器启动时,它会吐出这些行:

[ 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'

当我提出请求时,我得到了这个:

[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 个答案:

答案 0 :(得分:2)

春天有魔力;参数顺序很重要。 尝试颠倒处理程序参数的顺序。 而不是:

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

试试这个:

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

拿2:

尝试将web.xml中的映射更改为更通用的映射。例如:

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

我在我的测试web.xml中使用了/spring/*,在我的控制器中使用了@RequestMapping(“/ blammy / {value}”),然后将请求发送到“{WEBROOT} / spring / blammy / testvalue” “{WEBROOT}”是我的测试应用的网络根源。

答案 1 :(得分:0)

请求映射相对于调度程序servlet映射(默认情况下,请参阅alwaysUseFullPathDefaultAnnotationHandlerMapping中的AnnotationMethodHandlerAdapter属性以获取更多信息。)

如果您请求资源/ViewReport/jayson/fnord,则Spring mvc正在寻找映射到fnord的处理程序。

您需要申请/ViewReport/jayson/ViewReport/jayson/fnord

答案 2 :(得分:0)

您是否尝试过 @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");  
}