Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke RESTful webservice with parameter

I have a simple RESTful web service that print "Hello World !" I'm using NetBeans and the code looks like:

package resource;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;


@Path("simple")
public class SimpleResource {

    @Context
    private UriInfo context;

    /** Creates a new instance of SimpleResource */
    public SimpleResource() {
    }

    @GET
    @Produces("application/xml")
    public String getXml() {
        //TODO return proper representation object
        return "<greeting>Hello World !</greeting>";
    }

    @PUT
    @Consumes("application/xml")
    public void putXml(String content) {
    }
}

I call this web service from this URL : http://localhost:8080/WebService/resources/simple. Now, I want to send a parameter to this web service, then print this parameter after the "Hello world" message.

How can we do that?

Thanks!

like image 286
M.M Avatar asked Jul 03 '11 06:07

M.M


People also ask

How do you pass parameters to rest web service?

If you need to pass multiple parameters, either use multiple @PathParam separated by / or use @QueryParam .

How do I pass parameter values in REST API?

A REST API can have parameters in at least two ways: As part of the URL-path (i.e. /api/resource/parametervalue ) As a query argument (i.e. /api/resource? parameter=value )

How do you pass query parameters in REST client?

Query parameters are passed after the URL string by appending a question mark followed by the parameter name , then equal to (“=”) sign and then the parameter value. Multiple parameters are separated by “&” symbol. The same parameters passed as URL parameters in the previous example are passed as Query parameters here.


2 Answers

The two main ways of handling a parameter in REST are via parsing the path and via extracting the query part.

Path parameters

These handle this case — /foo/{fooID} — where {fooID} is a template that will be replaced by the parameter you want:

@GET
@Produces("text/plain")
@Path("/foo/{fooID}")
public String getFoo(@PathParam("fooID") String id) {
    // ...
}

These are great for the case where you can consider the parameter to be describing a resource.

Query parameters

These handle this case — /?foo=ID — just like you'd get from doing traditional form processing:

@GET
@Produces("text/plain")
@Path("/")
public String getFoo(@QueryParam("foo") String id) {
    // ...
}

These are great for the case where you consider the parameter to be describing an adjunct to the resource, and not the resource itself. The @FormParam annotation is extremely similar, except it is for handling a POSTed form instead of GET-style parameters

Other types of parameters

There are other types of parameter handling supported by the JAX-RS spec (matrix parameters, header parameters, cookie parameters) which all work in about the same way to the programmer, but are rarer or more specialized in use. A reasonable place to start exploring the details is the JAX-RS javadoc itself, as that has useful links.

like image 181
Donal Fellows Avatar answered Oct 20 '22 22:10

Donal Fellows


The sample code for a web service which accepts parameters in URl will look like this:

@GET
@Path("/search")
public String getUserDetailsFromAddress(
              @QueryParam("name") String name) {
  return "Hello"+name;
}

and the URL will be like this:

http://localhost:8080/searchapp/mysearch/search?name=Tom
like image 40
Srijani Ghosh Avatar answered Oct 20 '22 20:10

Srijani Ghosh