缺少字符串类型的方法参数的 URI 模板变量
Missing URI template variable for method parameter of type String
这是我的 REST 控制器的样子
@RequestMapping(method = GET, path = "/type/{eventtype}/events")
@ResponseBody
public ResponseEntity<?> getEventsSpecificType(@PathVariable String eventType) throws InvalidRequestException {
return ResponseRestBuilder.createSuccessResponse(
portEventRepository.
findByEventTypeOrderByTimestampAsc
(eventType));
}
使用以下 URI 从 Postman 调用它
http://localhost:8181/type/default/events
给我这个错误
{
"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"
}
我错过了什么? String 变量没有正确映射吗?
你必须给出路径变量的名称,方法参数的名称无关紧要。并且路径变量的名称必须完全匹配您的路径(事件类型)中使用的名称。
getEventsSpecificType(@PathVariable("eventtype") String eventType)
如果不指定@PathVariable,名称必须与路径中使用的名称完全匹配。所以把eventType方法参数改成eventtype.
我也遇到了同样的问题并通过**更改路径变量的名称解决了
@RequestMapping(method = GET, path = "/type/{eventType}/events")
@ResponseBody
public ResponseEntity<?> getEventsSpecificType(@PathVariable String eventType) throws InvalidRequestException {
这是我的 REST 控制器的样子
@RequestMapping(method = GET, path = "/type/{eventtype}/events")
@ResponseBody
public ResponseEntity<?> getEventsSpecificType(@PathVariable String eventType) throws InvalidRequestException {
return ResponseRestBuilder.createSuccessResponse(
portEventRepository.
findByEventTypeOrderByTimestampAsc
(eventType));
}
使用以下 URI 从 Postman 调用它
http://localhost:8181/type/default/events
给我这个错误
{
"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"
}
我错过了什么? String 变量没有正确映射吗?
你必须给出路径变量的名称,方法参数的名称无关紧要。并且路径变量的名称必须完全匹配您的路径(事件类型)中使用的名称。
getEventsSpecificType(@PathVariable("eventtype") String eventType)
如果不指定@PathVariable,名称必须与路径中使用的名称完全匹配。所以把eventType方法参数改成eventtype.
我也遇到了同样的问题并通过**更改路径变量的名称解决了
@RequestMapping(method = GET, path = "/type/{eventType}/events")
@ResponseBody
public ResponseEntity<?> getEventsSpecificType(@PathVariable String eventType) throws InvalidRequestException {