11

I've built a REST webservice with some webmethods. But I don't get it to work passing parameters to these methods.

I.E.

@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? 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

3 Answers 3

10

If you are using SpringMVC for REST api development you can use

@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

}

Sign up to request clarification or add additional context in comments.

Comments

7

This tutorial should be of help. To include parameters you will need to use the @PathParam command as shown in this previous SO Post.

4 Comments

thank you helped me alot. But what if my method wants an array of Strings?
@Chris: This should help: stackoverflow.com/questions/5484209/…
Thank you very much. That link was also helpful to the context of your last link: stackoverflow.com/questions/5718575/…
There i have explain more method for this stackoverflow.com/a/70656772/7390856
2

This will help you

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

where formData is

MultivaluedMap formData = new MultivaluedMapImpl();


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

1 Comment

This looked promising for me, but ClientResponse does not seem to be available in Android...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.