简体   繁体   English

REST使用Java传递参数

[英]REST passing Parameters with Java

I've built a REST webservice with some webmethods. 我已经使用一些webmethods构建了一个REST Web服务。 But I don't get it to work passing parameters to these methods. 但是我没有把它传递给这些方法。

IE IE

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

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

How would I invoke that method and how to pass the parameters firstname and lastname? 我将如何调用该方法以及如何传递参数firstname和lastname? I tried something like this: 我试过这样的事情:

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

But where do I add the parameters? 但是我在哪里添加参数?

Thank you for your help, best regards, Chris 谢谢你的帮助,最好的问候,克里斯

If you are using SpringMVC for REST api development you can use 如果您使用SpringMVC进行REST API开发,则可以使用

@RequestParam("PARAMETER_NAME");

In case of jersey you can use 如果是运动衫,你可以使用

@QueryParam("PARAMETER_NAME");

Method look like this 方法看起来像这样

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

return "Hello " + firstname + " " + lastname

} }

This tutorial should be of help. 教程应该有所帮助。 To include parameters you will need to use the @PathParam command as shown in this previous SO Post. 要包含参数,您将需要使用@PathParam命令,如图之前的SO帖子。

This will help you 这会对你有所帮助

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

where formData is 其中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