-1

When i develop a servlet and overrivde the doGet method.I can access the value of the parameters passed in the URL like req.getParameter("myParam").the URL will be like http://mysite:8080/APP?myParam=123 . For the case of Rest Webservice(suppose the implementation is Jersey) if i make the Get Service like this

@Get
    @Path("myfunction")
    @Compress
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response myfunction(@QueryParam("myParam") String myParam)

The url will be like this http://mysite:8080/APP/myfunction/123

Both URl can be invoked using curl or postman.But i did not understand what's the difference between them?

And how can i make a Get Rest Service (using jersey) so that the URL will be like this http://mysite:8080/APP?myParam=123 and in backend i can get this myparam?

Thanks

BenMansourNizar
  • 1,558
  • 4
  • 21
  • 42
  • Note, `GET` method doesn't mean you need to pass query param. A simple url without any query param can also be using `GET` method. As said, I'm not sure, what is your actual question. – Ravi Dec 29 '17 at 07:55

2 Answers2

0

Since you are returning JSON so this same function can be used as webservice. In case of servlets, we usually redirect or load another page (ftl, jsp etc.) but here in this function since it expects you to produce the JSON as output so yes you can use it as service as well.

Another difference we usually face is that, the functions within the app are secure and we need to login before accessing a particular GET call.

Here you do not need any authentication and it's a plain GET call which outputs JSON so you can use this particular function as a webservice as well.

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • how can i make a Get Rest Service (using jersey) so that the URL will be like this http://mysite:8080/APP?myParam=123 and in backend i can get this myparam? (is it possible?) – BenMansourNizar Dec 29 '17 at 07:59
  • There is a detailed tutorial to do that where you will need to import jersy api and libs to make it work – Danyal Sandeelo Dec 29 '17 at 08:04
  • `RESTful` url patterns differ from using querystrings. Read [Representational_state_transfer#Relationship_between_URL_and_HTTP_methods](https://en.wikipedia.org/wiki/Representational_state_transfer#Relationship_between_URL_and_HTTP_methods) – Ravinder Reddy Dec 29 '17 at 08:04
  • @RavinderReddy agreed but this same url will return json which can then be parsed and utilized. – Danyal Sandeelo Dec 29 '17 at 08:09
-1

It's simple just use @queryparam then they will be the same. Example :

import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

    @Path("/users")
    public class UserService {

        @GET
        @Path("/query")
        public Response getUsers(
            @QueryParam("from") int from,
            @QueryParam("to") int to,
            @QueryParam("orderBy") List<String> orderBy) {

            return Response
               .status(200)
               .entity("getUsers is called, from : " + from + ", to : " + to
                + ", orderBy" + orderBy.toString()).build();

        }

    }

the URL pattern will be like 

    URI Pattern : “users/query?from=100&to=200&orderBy=age&orderBy=name”
BenMansourNizar
  • 1,558
  • 4
  • 21
  • 42
  • lol, if you already knew, then why did you posted question ? Even you posted question, then you should have your own answer posted not some link, which is already present somewhere. What if, this link got broken tomorrow ? – Ravi Dec 29 '17 at 08:04