Colon value (:) inside @Path annotation

I am new to Restful Services. I was looking through the code and found this line

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

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

+6
source share
2 answers
Designation

@Path supports normal strings to match a path or regular expression to match a pattern. In your case

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

seems to just fit the pattern

The path param {image} with any template, such as image /.*, which is basically converted to an image / something, something here does not refer to the word "nothing", but to its literal value, that is, to any valid text.

Correction: Refer to @Sotirios Delimanolis for details. Thanks to the assistant for entering corrections.

+4
source

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

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

From the documentation

URI path templates are URIs with variables embedded in the URI syntax. These variables are replaced at run time to allow the resource to respond to the request based on the replaced URI. Variables are indicated by braces ( { and } ) . For example, look at the after @Path annotations:

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

In this example, the user is prompted to enter his or her name, and then the JAX-RS web service configured to respond to requests for this URI template response. For example, if the user enters the username "Galileo", the web service responds to the following URL:

 http://example.com/users/Galileo 

To get the username value, the @PathParam annotation can be used for the parameter of the request method 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 further indicates the syntax to indicate

By default, the URI variable must match the regular expression "[^ /] +?" . This variable can be configured by specifying another regular expression after the variable name . For example, if the user name should consist only of lowercase and uppercase alphanumeric characters, overrides 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 only match usernames that start with a single uppercase or lowercase letter and zero or more alphanumeric characters and an underscore. If the username does not match this pattern, 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 regular expression

 image/.* 

This way, the JAX-RS environment will use your annotated method to query URIs matching

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

Presumably your method will have a parameter

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

and path will have the value "image/[anything]" .

+3
source

All Articles