Spring @在一个查询中使用PathVariable和@RequestParam
我有一个不起作用的请求Spring @在一个查询中使用PathVariable和@RequestParam,spring,rest,Spring,Rest,我有一个不起作用的请求 http://localhost:8070/promo/lock/1?reason=CONSUMED 这是我的端点配置 @RequestMapping(name = SERVICE_URL + "/{id}", method = RequestMethod.DELETE) @ResponseStatus(HttpStatus.NO_CONTENT) public void unlock(Authentication authentication, @RequestPar
http://localhost:8070/promo/lock/1?reason=CONSUMED
这是我的端点配置
@RequestMapping(name = SERVICE_URL + "/{id}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void unlock(Authentication authentication, @RequestParam(value = "reason") UnlockReason reason, @PathVariable Long id)
我有个例外
rg.springframework.web.bind.ServletRequestBindingException:缺少
类型为Long的方法参数的URI模板变量“id”
这有什么问题?注释映射有两个问题:
:它应该将URI路径作为“路径”而不是名称@RequestMapping
@RequestMapping(path = SERVICE_URL + "/{id}", method = RequestMethod.DELETE)
:应具有路径参数名称,因为默认值为@PathVariable
“”
public void unlock(Authentication authentication, @RequestParam(value = "reason") UnlockReason reason, @PathVariable Long id)
如果您想给它取其他名称,您需要通过以下方式完成:
public void unlock(Authentication authentication, @RequestParam(value = "reason") UnlockReason reason, @PathVariable("id") Long number)
我相信Spring 4通过
ParameterNameDiscoveryr
从方法参数名推断出@PathVariable
名称,因此不需要这样做。值得注意的是,path是值的别名,因此它们是等价的。另外,我相信@IlyaNovoseltsev是正确的,默认情况下路径变量参数名应该是占位符名(例如映射“/{id}”
和@PathVariable String id
应该可以工作)。@IlyaNovoseltsev我还不知道Spring 4,但感谢您的通知。
public void unlock(Authentication authentication, @RequestParam(value = "reason") UnlockReason reason, @PathVariable("id") Long number)