Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query Parameters In Jersey

Tags:

java

jersey

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!";
}     
like image 466
Maks.Burkov Avatar asked Apr 25 '16 23:04

Maks.Burkov


People also ask

What is query parameter in REST API?

You can use query parameters to control what data is returned in endpoint responses. The sections below describe query parameters that you can use to control the set of items and properties in responses, and the order of the items returned.

What is a query parameter in URL?

Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed.

What is difference between @RequestParam and @QueryParam?

What is main difference between @RequestParam and @QueryParam in Spring MVC controller? They're functionally the same: they let you bind the value of a named HTTP param to the annotated variable. That being said, the question is very broad, so you'll have to specify more detail if you want a more useful answer.


1 Answers

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

like image 165
stdunbar Avatar answered Oct 16 '22 23:10

stdunbar