簡體   English   中英

REST使用Java傳遞參數

[英]REST passing Parameters with Java

我已經使用一些webmethods構建了一個REST Web服務。 但是我沒有把它傳遞給這些方法。

IE

@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);

但是我在哪里添加參數?

謝謝你的幫助,最好的問候,克里斯

如果您使用SpringMVC進行REST API開發,則可以使用

@RequestParam("PARAMETER_NAME");

如果是運動衫,你可以使用

@QueryParam("PARAMETER_NAME");

方法看起來像這樣

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

return "Hello " + firstname + " " + lastname

}

教程應該有所幫助。 要包含參數,您將需要使用@PathParam命令,如圖之前的SO帖子。

這會對你有所幫助

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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM