Guru
Guru

Reputation: 75

@path and @queryparam in Jersey

Am developing a simple restful webservice and am a newbie to this. Hence I referred the basic tutorials and succesfully executed them.

Below is the sample code I wrote to proceed further

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

@Path("/text/{sso}")

public class Do {


      @GET
      @Produces(MediaType.TEXT_PLAIN)
      public String simple(@PathParam("ss") @QueryParam("d") String params) {
        return "Hello Jersey"+params;
      }

      @GET
      @Produces(MediaType.TEXT_PLAIN)
      public String advanced(@PathParam("as") @QueryParam("d") String params) {
        return "Hello Jersey-----"+params;
      }
}

What i basically need is to expose two different methods in the same class and access the appropriate one based on the url.

It throws the following error when doing the above way - com.sun.jersey.spi.inject.Errors$ErrorMessagesException Can someone please guide me if am on the right track? and if yes please tell me where am i missing out. If not what is the right approach for such scenarios?

Upvotes: 1

Views: 2976

Answers (2)

Perception
Perception

Reputation: 80593

You have two problems in your code.

  1. The @PathParam annotations need to be applied to an argument to your method
  2. The value of the @PathParam annotation needs to match a path segment.

The only path segment in your class is defined by this:

@Path("/text/{sso}")

And has the value 'sso'. Consequently, your code should look more like this:

@Path("/text/{sso}")
public class Do {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String simple(@PathParam("sso") String sso,
            @QueryParam("d") String params) {
        return "Hello Jersey" + params;
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String advanced(@PathParam("sso") String sso,
            @QueryParam("d") String params) {
        return "Hello Jersey-----" + params;
    }
}

Upvotes: 2

user1907906
user1907906

Reputation:

Your methods must have two arguments if you want to match two QueryParams.

public String simple(@PathParam("ss") String paramSs,
                     @PathParam("d") String paramD) {
  // ...
}

@PathParam is an annotation for one method argument.

Upvotes: 0

Related Questions