我正在编写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); } }
现在我需要再写一个web方法,它接受两个参数,一个是id,另一个是name.它应该如下.
public Person fetchPerson(String id, String name){ }
如何为上述方法编写Web方法?
谢谢!
您有两种选择 - 您可以将它们放在路径中,也可以将其作为查询参数.
即你想要它看起来像:
/{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.