Spring控制器中的PathVariable
我正在尝试映射 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 注释?
如何正确使用呢?
I'm trying to map the url /locations/{locationId}/edit.html - that seems to work with this code:
@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";
}
}
Call the mentioned url results in an exception:
java.lang.IllegalArgumentException: Name for argument type [int] not available, and parameter name information not found in class file either.
Am I using the @PathVariable Annotation in a wrong way?
How to use it correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它应该是 @PathVariable("locationId") int locationId
it should be
@PathVariable("locationId") int locationId
您应该将
value
参数添加到@PathVariable
,例如,You should add the
value
argument to your@PathVariable
, e.g.,JDK 7 启用parametername introspection
JDK7 中可以使用parametername 暴露,否则必须在Annotation 中设置它。
您应该在显式使用 JDK 说明(如 Johan 和 Moniul 建议)作为注释的一部分之前使用它,因为如果您想更改参数键,则只需编辑变量名称,而不是注释规范中的任何其他出现在其他线路和/或班级中。我们称之为单一事实来源。
JDK 7 enables parametername introspection
Parametername exposition is available in JDK7, otherwise you must set it in the Annotation.
You should use the JDK exposition before explicitly use it (like Johan and Moniul suggested) as part of the annotation because if you like to change the parameter-key you need to edit only the variable-name and not any other occourences in annotation-specifications in other lines and/or classes. Lets call it single-source-of-truth.