简体   繁体   English

@Path注释中冒号(:)的含义

[英]Meaning of colon(:) inside @Path annotation

I am new to Restful services. 我是Restful服务的新手。 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? 有人可以解释一下上述语法的含义和用法吗?

@Path notation supports normal strings to match the path or a regex to match a pattern. @Path表示法支持匹配路径的正常字符串或匹配模式的正则表达式。 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 ie any valid text. 路径参数{image}具有任何模式,如image /.*,它基本上转换为图像/任何东西,这里的任何东西都不是指“任何”这个词,而是它的字面意思,即任何有效的文本。

Correction: Refer to @Sotirios Delimanolis answer for complete details. 更正:有关完整详细信息,请参阅@Sotirios Delimanolis答案。 Thanks mate for correction input. 谢谢配偶纠正输入。

The notation is known as URI path templates and described in the documentation . 该表示法称为URI路径模板,并在文档中进行了描述

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. JX-RS环境将绑定从请求的URI到声明的@PathParam处理程序方法参数的相应路径段。

From the documentation 从文档中

URI path templates are URIs with variables embedded within the URI syntax. URI路径模板是URI,其中包含嵌入在URI语法中的变量。 These variables are substituted at runtime in order for a resource to respond to a request based on the substituted URI. 这些变量在运行时被替换,以便资源基于替换的URI响应请求。 Variables are denoted by braces ( { and } ) . 变量用大括号( {} )表示 For example, look at the following @Path annotation: 例如,查看以下@Path注释:

 @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. 在这种示例中,提示用户键入他或她的姓名,然后配置为响应对此URI路径模板的请求的JAX-RS Web服务进行响应。 For example, if the user types the user name “Galileo,” the web service responds to the following URL: 例如,如果用户键入用户名“Galileo”,则Web服务会响应以下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: 要获取用户名的值,可以在请求方法的方法参数上使用@PathParam注释 ,如以下代码示例所示:

 @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 "[^/]+?" 默认情况下,URI变量必须与正则表达式“[^ /] +?”匹配 . 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. 在此示例中,username变量将仅匹配以一个大写或小写字母开头的用户名以及零个或多个字母数字字符和下划线字符。 If a user name does not match that template, a 404 (Not Found) response will be sent to the client. 如果用户名与该模板不匹配,则会向客户端发送404(未找到)响应。

So your example 所以你的榜样

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

defines a URI template variable named image that contains a segment matching the regex 定义名为image的URI模板变量,其中包含与正则表达式匹配的段

image/.*

The JAX-RS environment will therefore use your annotated method for requests to URIs matching 因此,JAX-RS环境将使用带注释的方法来处理URI匹配的请求

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]" . path的值为"image/[anything]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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