Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restful Path Usage with "/"

Tags:

java

rest

path

uri

Is there a difference between @Path having value starting with "/" and without it

I have tested both usage, all work properly.

@Path("message")
public class MessageServices {

    @PUT
    @Path("sendsms")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces({MediaType.APPLICATION_JSON})
    public Response sendSms() {
        //....
    }

}



@Path("/message")
public class MessageServices {

    @PUT
    @Path("/sendsms")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces({MediaType.APPLICATION_JSON})
    public Response sendSms() {
        //....
    }

}
like image 247
Ahmet Karakaya Avatar asked Sep 24 '14 11:09

Ahmet Karakaya


2 Answers

I've never seen any difference. And the documentation for @Path (http://docs.oracle.com/javaee/7/api/javax/ws/rs/Path.html) says:

Paths are relative. For an annotated class the base URI is the application path, see ApplicationPath. For an annotated method the base URI is the effective URI of the containing class. For the purposes of absolutizing a path against the base URI , a leading '/' in a path is ignored and base URIs are treated as if they ended in '/'.

So there should not be any difference.

like image 92
Benjamin Avatar answered Oct 09 '22 15:10

Benjamin


According to this tutorial:

A @Path value isn’t required to have leading or trailing slashes (/). The JAX-RS runtime parses URI path templates the same whether or not they have leading or trailing spaces.

So it seems like there is no difference.

P.S. I've seen a minor difference once upon a time that was not directly related to JAX-RS runtime itself. One buggy documentation generation framework was generating ugly double-slashed resource links in case of leading slashes in @Path

like image 37
Konstantin V. Salikhov Avatar answered Oct 09 '22 13:10

Konstantin V. Salikhov