繁体   English   中英

使用两个参数实现RESTful Web服务?

[英]Implementing RESTful web service with two parameters?

我正在编写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 您可以混合使用PathParamRequestParam

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM