Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Jersey support dollar sign in Path annotation of JAX-RS?

I would like to be able to access the following rest URLs:

  • http://localhost:9998/helloworld
  • http://localhost:9998/helloworld/$count

The first URL works fine. I am having trouble with the $count URL using Jersey implementation of JAX-RS.

Here is the code for the resource.

@Path("/helloworld")
public class HelloWorldResource {
    @GET
    @Produces("text/plain")
    public String getClichedMessage() {
        return "Hello World!";
    }

    @GET
    @Path("\\$count")
    @Produces("text/plain")
    public String getClichedMessage(
            @PathParam("\\$count") String count) {

        return "Hello count";
    }
}

I've also tried "$count" in both @Path and @PathParam but that didn't work either.

Note: If I remove the dollar sign from all the code above then it works fine for the URL localhost:9998/helloworld/count. However I need the dollar sign to be there in the URL because this will be an OData producer application.

like image 857
Jerome Avatar asked Oct 09 '22 01:10

Jerome


2 Answers

Found the answer. Placing the dollar sign in a character class did the trick.

@GET
@Path("{count : [$]count(/)?}")
@Produces("text/plain")
public String getClichedMessageCount(
        @PathParam("count") String count) {

    return "Hello count";
}

The above matches the following URLs.

  • localhost:9998/helloworld/$count
  • localhost:9998/helloworld/$count/
  • localhost:9998/helloworld/$count?$filter=blah
  • localhost:9998/helloworld/$count/?$filter=blah
like image 101
Jerome Avatar answered Oct 13 '22 09:10

Jerome


Dollar signs are special characters in URLs, and need to be encoded as such, I'm afraid:

http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

The character you're looking for is %24, if you're interested, though if you're in java, reading up on the java.net.URI class might be worth it. I've not played with Jersey, but Java is more than capable of doing the hard work for you here.

like image 38
Rohaq Avatar answered Oct 13 '22 09:10

Rohaq