10

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!

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
M.M
  • 1,343
  • 7
  • 20
  • 49

3 Answers3

23

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.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • Thank you Donal , but still how can I extract the "fooID" parameter and print it for example ? – M.M Jul 03 '11 at 06:53
  • My example code _extracts_ and _decodes_ the parameter and _passes it as an argument_ to the method, whichever style you're using. All you have to do is write the bit that decides what to do with it; for demonstration purposes, concatenating it together in a string with a bit of boilerplate and returning it will work fine. Surely you can write that little bit? – Donal Fellows Jul 03 '11 at 07:07
  • Thanks , this is really helpful Donal just one thing , for my application URL , this one will work right ? http://localhost:8080/WebService/resources/simple/idValue – M.M Jul 03 '11 at 07:14
  • @Mohammed: Yes. You might need a longer value in your `@Path` annotation to describe the path to parse, but it's really very straight-forward. Just write the path, mark the slot that you want filled in so as to give it a name, and bind the name to an argument with `@PathParam`. – Donal Fellows Jul 03 '11 at 07:18
3

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
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Srijani Ghosh
  • 3,935
  • 7
  • 37
  • 68
0

Try adding a Path annotation like this:

@javax.ws.rs.Path(“/bookstore/books/{bookId}”)
Maurice
  • 27,582
  • 5
  • 49
  • 62