简体   繁体   中英

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

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

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.

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

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.

Question is long solved but maybe this will help someone in the future looking for a similar problem.

The way we found when dealing with this problem is to write a class which replaces the encoded sign back to the dollar sign itself. We registered the class in our RestEasyClient.

public class LoggingFilter implements ClientRequestFilter{

@Override
public void filter(ClientRequestContext context) throws IOException {
   //replace uri in context...
   String uri = context.getUri();
   //regex to replace $ sign in uri
   //set new uri in context so request goes to correct url
   context.setUri(uri);
}

You are using the wrong slash in the @Path

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

    return "Hello count";
}

Also that is not the correct way to use PathParam. If you are trying to retrieve the value after /helloworld you should do the following

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

    return "Hello count";
}

Edit Couldn't get it to work with a $

@Path("count") // works
@Path("/count") // works
@Path("\\count") // does not work
@Path("$count") // does not work
@Path("/$count") // does not work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM