Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of colon(:) inside @Path annotation

Tags:

java

rest

I am new to Restful services. I was going through a code and found this line

@GET

@Path("{image:image/.*}")

Can someone please explain the meaning and use of the above syntax?

like image 489
Jordan Avatar asked Dec 17 '15 06:12

Jordan


2 Answers

@Path notation supports normal strings to match the path or a regex to match a pattern. In your case

@Path("{image:image/.*}")

seems to be just matching a pattern of

Path param {image} with any pattern like image/.*, which basically translates to image/anything , anything here does not refer to the word 'anything' but its literal meaning i.e. any valid text.

Correction: Refer to @Sotirios Delimanolis answer for complete details. Thanks mate for correction input.

like image 185
Juned Ahsan Avatar answered Oct 20 '22 00:10

Juned Ahsan


The notation is known as URI path templates and described in the documentation.

You define a new template variable by declaring it within brackets {}. The JX-RS environment will bind the corresponding path segment from the requested URI to a declared @PathParam handler method parameter.

From the documentation

URI path templates are URIs with variables embedded within the URI syntax. These variables are substituted at runtime in order for a resource to respond to a request based on the substituted URI. Variables are denoted by braces ({ and }). For example, look at the following @Path annotation:

@Path("/users/{username}")

In this kind of example, a user is prompted to type his or her name, and then a JAX-RS web service configured to respond to requests to this URI path template responds. For example, if the user types the user name “Galileo,” the web service responds to the following URL:

http://example.com/users/Galileo

To obtain the value of the user name, the @PathParam annotation may be used on the method parameter of a request method, as shown in the following code example:

@Path("/users/{username}")
public class UserResource {

    @GET
    @Produces("text/xml")
    public String getUser(@PathParam("username") String userName) {
        ...
    }
}

The documentation then goes on to specify the syntax for the notation

By default, the URI variable must match the regular expression "[^/]+?". This variable may be customized by specifying a different regular expression after the variable name. For example, if a user name must consist only of lowercase and uppercase alphanumeric characters, override the default regular expression in the variable definition:

@Path("users/{username: [a-zA-Z][a-zA-Z_0-9]}")

In this example the username variable will match only user names that begin with one uppercase or lowercase letter and zero or more alphanumeric characters and the underscore character. If a user name does not match that template, a 404 (Not Found) response will be sent to the client.

So your example

@Path("{image:image/.*}")

defines a URI template variable named image that contains a segment matching the regex

image/.*

The JAX-RS environment will therefore use your annotated method for requests to URIs matching

http://somehost.com/context-path/image/[anything]

Presumably, your method would have a parameter

@Path("{image:image/.*}")
public Response handle(@PathParam("image") String path) { /* handling */ }

and path would have a value of "image/[anything]".

like image 21
Sotirios Delimanolis Avatar answered Oct 20 '22 00:10

Sotirios Delimanolis