2012-03-28 114 views
8

我已经用一些webmethods构建了一个REST webservice。 但我没有得到它的工作传递参数到这些方法。REST传递参数与Java

I.E.

@GET 
@Path("hello") 
@Produces(MediaType.TEXT_PLAIN) 
public String hello(String firstName, String lastName){ 

    return "Hello " + firstname + " " + lastname 
} 

我该如何调用该方法以及如何传递参数firstname和lastname? 我试过这样的:

ClientConfig config = new DefaultClientConfig(); 

Client client = Client.create(config); 

WebResource service = client.resource(getBaseURI()); 

ClientResponse response = service.path("hello") 
.accept(MediaType.TEXT_PLAIN).put(ClientResponse.class); 

但我在哪里添加参数?

感谢你的帮助, 最好的问候, 克里斯

回答

6

This教程应该会有帮助。要包含参数,您需要使用@PathParam命令,如this之前的SO Post所示。

+0

谢谢你帮了我很多。但是如果我的方法想要一个字符串数组呢? – Chris 2012-03-28 11:54:51

+0

@Chris:这应该有所帮助:http://stackoverflow.com/questions/5484209/pass-array-as-a-parameter-in-restful-webservice – npinti 2012-03-28 11:56:09

+0

非常感谢。该链接也有助于您的最后一个链接的上下文:http://stackoverflow.com/questions/5718575/how-can-i-grab-all-query-parameters-in-jersey-jaxrs – Chris 2012-03-28 12:11:18

8

如果使用用SpringMVC对REST API开发可以使用

@RequestParam("PARAMETER_NAME"); 

在球衣的情况下,你可以使用

@QueryParam("PARAMETER_NAME"); 

方法是这样的

public String hello(@RequestParam("firstName")String firstName, @RequestParam("lastName")String lastName){ 

return "Hello " + firstname + " " + lastname 

}

2

这将帮助你

ClientResponse response = resource.queryParams(formData).post(ClientResponse.class, formData); 

其中FORMDATA是

MultivaluedMap formData = new MultivaluedMapImpl(); 


formData.add("Key","Value"); 
formData.add("Key","Value"); 
... 
... 
... 
formData.add("Key","Value"); 
+0

这看起来很有希望,但ClientResponse在Android中似乎不可用... – 2014-04-24 22:59:34