Reputation: 626
I have a question about the query parameter.. What is the idea of that parameter.. In the case from below for what i need the query parameter ?
@GET
@Produces("text/plain")
public String sayHello(@QueryParam("name") String name) {
if (name != null) {
// if the query parameter "name" is there
return "Hello " + name + "!";
}
return "Hello World!";
}
Upvotes: 6
Views: 6480
Reputation: 17435
@PathParam is used when you have a service that is defined like:
@POST
@Path("/update/{userCode}")
public Response update(@PathParam( "userCode" ) String userCode)
in this example, the URL would be something like http://hostname.tld/update/1234 where the "1234" would be parsed out of the path portion of the URL.
@QueryParam is when your URL includes normal URL parameters like @Partha suggests:
@POST
@Path("/update")
public Response update(@QueryParam( "userCode" ) String userCode)
here the URL would look like http://hostname.tld/update?userCode=1234
Which one you use depends on the style you'd like. REST aficionados would tell you that you should never use the QueryParam version. I'm a bit more flexible - the advantage of the QueryParam version is that you are not locked into an ordering, just names.
But it is ultimately up to you which makes more sense for your application.
Upvotes: 10
Reputation: 310
So if your REST url is http://somecompany.com/api/user?name=Maks you can process that information here and use that name in whatever you want to do in the method you have written.
Upvotes: 0