Java 使用两个参数实现 RESTful Web 服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20744332/
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
Implementing RESTful web service with two parameters?
提问by user755806
I am writing Jersey RESTful web services. I have below two web methods.
我正在编写 Jersey RESTful Web 服务。我有以下两种网络方法。
@Path("/persons")
public class PersonWS {
private final static Logger logger = LoggerFactory.getLogger(PersonWS.class);
@Autowired
private PersonService personService;
@GET
@Path("/{id}")
@Produces({MediaType.APPLICATION_XML})
public Person fetchPerson(@PathParam("id") Integer id) {
return personService.fetchPerson(id);
}
}
Now i need to write one more web method which takes two parameters one is id and one more is name. It should be as below.
现在我需要再写一个 web 方法,它接受两个参数,一个是 id,另一个是 name。它应该如下所示。
public Person fetchPerson(String id, String name){
}
How can i write a web method for above method?
我如何为上述方法编写网络方法?
Thanks!
谢谢!
采纳答案by Tim B
You have two choices - you can put them both in the path or you can have one as a query parameter.
您有两种选择 - 您可以将它们都放在路径中,也可以将其中一个作为查询参数。
i.e. do you want it to look like:
即你希望它看起来像:
/{id}/{name}
or
或者
/{id}?name={name}
For the first one just do:
对于第一个,只需执行以下操作:
@GET
@Path("/{id}/{name}")
@Produces({MediaType.APPLICATION_XML})
public Person fetchPerson(
@PathParam("id") Integer id,
@PathParam("name") String name) {
return personService.fetchPerson(id);
}
For the second one just add the name as a RequestParam
. You can mix PathParam
s and RequestParam
s.
对于第二个,只需将名称添加为RequestParam
. 您可以混合使用PathParam
s 和RequestParam
s。