Spring Controller中的PathVariable

dtr*_*unk 23 java spring controller path-variables spring-mvc

我正在尝试映射网址/locations/{locationId}/edit.html - 这似乎与此代码一起使用:

@Controller
@RequestMapping( "/locations" )
public class LocationController
{
  @RequestMapping( value = "/{locationId}/edit.html", method = RequestMethod.GET )
  public String showEditForm( Map<String, Object> map, @PathVariable int locationId )
  {
    map.put( "locationId", locationId );
    return "locationform";
  }
}
Run Code Online (Sandbox Code Playgroud)

调用提到的url结果会出现异常:

java.lang.IllegalArgumentException: Name for argument type [int] not available, and parameter name information not found in class file either.
Run Code Online (Sandbox Code Playgroud)

我是否以错误的方式使用@PathVariable Annotation?

如何正确使用?

Moi*_*ain 37

它应该是 @PathVariable("locationId") int locationId

  • 这里详细说明,并且在没有调试信息的情况下编译代码时会发生这种情况(http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html):**if URI模板变量名称与方法参数名称匹配,您可以省略该详细信息.只要没有调试信息就编译代码,Spring MVC会将方法参数名称与URI模板变量名称**匹配 (8认同)

Joh*_*erg 16

您应该将value参数添加到您的@PathVariable,例如,

 public String showEditForm(
       @PathVariable("locationId") int locationId,
       Map<String, Object> map) {
    // ...
 }
Run Code Online (Sandbox Code Playgroud)