Spring Controller中的PathVariable

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

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

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

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

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

如何正确使用?

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

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

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

JDK 7支持参数化内省

参数名称展示在JDK7中可用,否则您必须在注释中设置它。

您应该在明确使用之前使用JDK博览会(如Johan和Moniul建议的那样)作为注释的一部分,因为如果您想更改参数键,则只需要编辑变量名而不是注释规范中的任何其他语句。在其他行和/或类中。 让我们称之为单一来源。