2013-12-23 53 views
4

我正在编写Jersey RESTful Web服务。我有以下两种网络方法。使用两个参数实现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); 
    } 


} 

现在我需要再写一个web方法,它需要两个参数,一个是id,一个是name。它应该如下。

public Person fetchPerson(String id, String name){ 

} 

我该如何编写上述方法的Web方法?

谢谢!

回答

16

你有两种选择 - 你可以把它们都放在路径中,或者你可以有一个作为查询参数。

即你希望它看起来像:

/{id}/{name} 

/{id}?name={name} 

对于第一个只是做:

@GET 
@Path("/{id}/{name}") 
@Produces({MediaType.APPLICATION_XML}) 
public Person fetchPerson(
      @PathParam("id") Integer id, 
      @PathParam("name") String name) { 
    return personService.fetchPerson(id); 
} 

对于第二个刚才添加的名称作为RequestParam。您可以混合使用PathParam s和RequestParam s。

+0

蒂姆,在第一种情况下,我是否需要发送请求为:http://somedomain.com/App/persons/2/3?谢谢! – user755806

+0

是的,没错(假设你想让id为2,名字为3)。 –

+0

是啊..有没有什么办法可以像下面那样传递请求? somedomain.com/App/persons/2_somename? – user755806