Java Spring Rest Controller,覆盖方法论点上的路径变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33651648/
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
Spring Rest Controller, Path Variables on an overriden method's arguement
提问by Rafik BELDI
I have a controller annotated with @RestController
and it implements an interface:
我有一个带有注释的控制器 @RestController
,它实现了一个接口:
public interface ContratEndpoint {
String ROOT = "/api/contrats";
String GET_CONTRAT = "";
String GET_CONTRAT_PER_PK = "/{idContrat}";
@RequestMapping(value = GET_CONTRAT)
Contrat getContrat(@RequestParam(value = "contratId")Long contratId);
@RequestMapping(value = GET_CONTRAT_PER_ID)
ExtContrat getContratById(@PathVariable("idContrat") Long idContrat);
}
The controller:
控制器:
@RestController
@RequestMapping(value = ContratEndpoint.ROOT)
public class ContratController implements ContratEndpoint {
//Injecting Services....
@Resource
private Mapper mapper;
@Override
public Contrat getContrat(Long contratId) {
return mapper.map(contratService.get(contratId),Contrat.class);
}
@Override
public ExtContrat getContratById(@PathVariable("idContrat") Long idContrat){
Preconditions.checkArgument(idContrat !=null);
return mapper.map(contratService.get(idContrat),ExtContrat.class);
}
.The above Code works just fine.
. 上面的代码工作得很好。
. But For the first inherited method , I didn't have to annotate arguments with @RequestParam
and it worked just fine.
. 但是对于第一个继承的方法,我不必注释参数,@RequestParam
它工作得很好。
As for the second method I tried at first :
至于我一开始尝试的第二种方法:
@Override
public ExtContrat getContratById(Long idContrat){
Preconditions.checkArgument(idContrat !=null);
return mapper.map(contratService.get(idContrat),ExtContrat.class);
}
. I expected the same behaviour Like the first Method, But i was wrong and the code ended up firing an IllegalArgumentException
because of the check in ligne Preconditions.checkArgument(idContrat!=null)
.
. 我期望与第一种方法相同的行为,但我错了,IllegalArgumentException
由于检查了 ligne ,代码最终触发了 an Preconditions.checkArgument(idContrat!=null)
。
My question is what is so specific about @PathVariable
that i've missed ?
Or is it just something is wrong with my approach?
我的问题是@PathVariable
我遗漏了什么?或者只是我的方法有问题?
Thanks.
谢谢。
回答by Viresh
There is difference between Request param and path variable,seee below post that you can confirm with your uri the cause for the exception :
请求参数和路径变量之间存在差异,请参阅下面的帖子,您可以使用您的 uri 确认异常的原因:
@PathVariable is to obtain some placeholder from the uri (Spring call it an URI Template) — see Spring Reference Chapter 16.3.2.2 URI Template Patterns
@RequestParam is to obtain an parameter — see Spring Reference Chapter 16.3.3.3 Binding request parameters to method parameters with @RequestParam
Assume this Url http://localhost:8080/SomeApp/user/1234/invoices?date=12-05-2013(to get the invoices for user 1234 for today)
假设这个 URL http://localhost:8080/SomeApp/user/1234/invoices?date=12-05-2013(获取今天用户 1234 的发票)
@RequestMapping(value="/user/{userId}/invoices", method = RequestMethod.GET)
public List<Invoice> listUsersInvoices(
@PathVariable("userId") int user,
@RequestParam(value = "date", required = false) Date dateOrNull) {
...
}