Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST passing Parameters with Java

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

like image 672
Chris Avatar asked Mar 28 '12 11:03

Chris


People also ask

Can we pass parameters in Java?

Parameters and ArgumentsInformation can be passed to methods as parameter. Parameters act as variables inside the method.

Can I use REST API in Java?

The code for REST APIs can be written in multiple languages but Java Programming Language due to its “write once run anywhere” quality is preferred for creating these APIs. This article will introduce you to the Java Programming Language and REST APIs along with their key features.


1 Answers

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

}

like image 69
kundan bora Avatar answered Sep 17 '22 17:09

kundan bora