Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Restful web service with input parameters?

I am creating restful web service and i wanted to know how do we create a service with input parameters and also how to invoke it from a web browser.

For example

@Path("/todo")
public class TodoResource {
    // This method is called if XMLis request
    @PUT
    @Produces( {MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
    public Todo getXML() {
        Todo todo = new Todo();
        todo.setSummary("This is my first todo");
        todo.setDescription("This is my first todo");
        return todo;
    }

and i can invoke it using http://localhost:8088/JerseyJAXB/rest/todo

and I want to create a method like

@Path("/todo")
    public class TodoResource {
        // This method is called if XMLis request
        @PUT
        @Produces( {MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
        public Todo getXML(String x, String y) {
            Todo todo = new Todo();
            todo.setSummary(x);
            todo.setDescription(y);
            return todo;
        }

In case of soap based web services i would invoke it like this

http://localhost:8088/JerseyJAXB/rest/todo?x=abc&y=pqr

but I want to know how to invoke it using rest and also can I pass the parameters as I am doing in the above example when I use rest and jersey.

like image 674
SSG Avatar asked Sep 22 '11 12:09

SSG


4 Answers

You can. Try something like this:

@Path("/todo/{varX}/{varY}")
@Produces({"application/xml", "application/json"})
public Todo whatEverNameYouLike(@PathParam("varX") String varX,
    @PathParam("varY") String varY) {
        Todo todo = new Todo();
        todo.setSummary(varX);
        todo.setDescription(varY);
        return todo;
}

Then call your service with this URL;
http://localhost:8088/JerseyJAXB/rest/todo/summary/description

like image 66
perissf Avatar answered Sep 18 '22 02:09

perissf


If you want query parameters, you use @QueryParam.

public Todo getXML(@QueryParam("summary") String x, 
                   @QueryParam("description") String y)

But you won't be able to send a PUT from a plain web browser (today). If you type in the URL directly, it will be a GET.

Philosophically, this looks like it should be a POST, though. In REST, you typically either POST to a common resource, /todo, where that resource creates and returns a new resource, or you PUT to a specifically-identified resource, like /todo/<id>, for creation and/or update.

like image 22
dbreaux Avatar answered Sep 19 '22 02:09

dbreaux


Be careful. For this you need @GET (not @PUT).

like image 44
Oliver Raupach Avatar answered Sep 19 '22 02:09

Oliver Raupach


another way to do is get the UriInfo instead of all the QueryParam

Then you will be able to get the queryParam as per needed in your code

@GET
@Path("/query")
public Response getUsers(@Context UriInfo info) {

    String param_1 = info.getQueryParameters().getFirst("param_1");
    String param_2 = info.getQueryParameters().getFirst("param_2");


    return Response ;

}
like image 36
Ashish Shetkar Avatar answered Sep 17 '22 02:09

Ashish Shetkar