java 缺少字符串类型的方法参数的 URI 模板变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48785300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Missing URI template variable for method parameter of type String
提问by Pierangelo Calanna
This is what my REST controller looks like
这就是我的 REST 控制器的样子
@RequestMapping(method = GET, path = "/type/{eventtype}/events")
@ResponseBody
public ResponseEntity<?> getEventsSpecificType(@PathVariable String eventType) throws InvalidRequestException {
return ResponseRestBuilder.createSuccessResponse(
portEventRepository.
findByEventTypeOrderByTimestampAsc
(eventType));
}
Calling it from Postman with the following URI
使用以下 URI 从 Postman 调用它
http://localhost:8181/type/default/events
gets me this error
给我这个错误
{
"timestamp": 1518605163296,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.web.bind.MissingPathVariableException",
"message": "Missing URI template variable 'eventType' for method parameter of type String",
"path": "/type/default/events"
}
}
What am I missing? Isn't the String variable properly mapped?
我错过了什么?字符串变量没有正确映射吗?
回答by Mathias G.
You must give the name of the path variable, the name of the method parameter doesn't matter. And the name of the path variable must match exactlythe name used in your Path (eventtype).
您必须给出路径变量的名称,方法参数的名称无关紧要。并且路径变量的名称必须与您的路径(事件类型)中使用的名称完全匹配。
getEventsSpecificType(@PathVariable("eventtype") String eventType)
If you don't specify the @PathVariable, the name must match exactly the name used in the path. So change the eventType method parameter to eventtype.
如果不指定@PathVariable,则名称必须与路径中使用的名称完全匹配。因此将 eventType 方法参数更改为 eventtype。