弹簧控制器中的路径可变

我正在尝试映射url /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";
  }
}

调用提到的 url 会导致异常:

java.lang.IllegalArgumentException: Name for argument type [int] not available, and parameter name information not found in class file either.

我是否以错误的方式使用了@PathVariable注释?

如何正确使用它?


答案 1

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


答案 2

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

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