Spring MVC requestmapping处理程序方法中{id:.+}的含义是什么? [英] What is the meaning of {id:.+} in a Spring MVC requestmapping handler method?

查看:406
本文介绍了Spring MVC requestmapping处理程序方法中{id:.+}的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器中遇到了一个方法.这是什么ID:.+ ??

I came across a method in the controller. What is this id:.+ ??

@RequestMapping(value="/index/{endpoint}/{type}/{id:.+}", method=RequestMethod.POST, consumes=kContentType, produces=kProducesType)
@ResponseBody
public String indexData(@PathVariable(value="endpoint") String endpoint, @PathVariable(value="type") String type, @PathVariable(value="id") String id, @RequestBody String body, HttpServletRequest request) {
    logger.debug("In web controller for endpoint " + endpoint);
    return indexController.indexData(endpoint, type, id, body,  getSecurityContextProvider(request));
}

推荐答案

spring MVC控制器requestmapping中路径变量的语法为{variable_name:regular_expression}.您可以选择省略正则表达式,从而导致您经常看到的内容,{id}.

The syntax of a path variable in a spring MVC controller requestmapping is {variable_name:regular_expression}. You can optionally omit the regular expression, which leads to what you see more often, {id}.

因此,对于示例/index/{endpoint}/{type}/{id:.+},变量名称为id,正则表达式为.+(请参阅下面对spring docs的引用).

So, for the example /index/{endpoint}/{type}/{id:.+} the variable name is id and the regular expression is .+ (see below reference to spring docs).

正则表达式.+说明将元字符.匹配一次或多次".这 '.'元字符代表任何字符,包括空格(尽管某些实现不匹配换行符).请参见 http://en.wikipedia.org/wiki/Regular_expression

The regular expression .+ is stating "match the metacharacter . one or more times". The '.' metacharacter represents any character including white space (though some implementations will not match newlines). See http://en.wikipedia.org/wiki/Regular_expression

正则表达式用于帮助Spring确定变量的值,因为您可以使用复杂的变量名,或者路径的末尾可能还有其他重要信息,如果Spring只是将其吸收到变量值中,说走到路径的尽头". (例如,文件扩展名或路径变量).

The regular expression is being used to help Spring determine the value of the variable because you can have complex variable names or there might be other important information at the end of the path that would otherwise get sucked into the variable value if Spring just said "go until the end of the path" (eg. filename extensions or path variables).

在您的示例中,id变量可能包含特殊字符,否则这些特殊字符会导致Spring提前终止该变量.在尝试使用包含文件扩展名(foobar.jpg)的文件名之前,我已经遇到了这个问题. Spring只会返回"foobar"变量的一部分,因为Spring假设我希望它在句点定界符处终止变量值.因此,在这种情况下,要确保将"id"匹配完整值,则放置告诉Spring继续进行正则表达式,并匹配最后一个正斜杠和路径末尾之间的所有内容的正则表达式. SO参考: Spring MVC @PathVariable被截断

It's possible that in your example, the id variable can contain special characters that would otherwise cause Spring to terminate the variable prematurely. I've run into this problem before when trying to use a filename that contained a file extension (foobar.jpg). Spring would return only the "foobar" part of the variable because Spring was assuming I wanted it to terminate the variable value at the period delimiter. So, in this case, to make sure that "id" matches the full value, you put the regex that tells Spring to go ahead and match everything between the last forward slash and the end of the path. SO Reference: Spring MVC @PathVariable getting truncated

这是Spring文档的摘录,涉及复杂的变量匹配:

Here's the excerpt from the Spring docs that deals with complex variable matching:

有时,在定义URI模板变量时需要更高的精度.考虑URL"/spring-web/spring-web-3.0.5.jar".您如何将其分解为多个部分?

Sometimes you need more precision in defining URI template variables. Consider the URL "/spring-web/spring-web-3.0.5.jar". How do you break it down into multiple parts?

@RequestMapping注释支持在URI模板变量中使用正则表达式.语法为{varName:regex},其中第一部分定义变量名称,第二部分定义正则表达式."

The @RequestMapping annotation supports the use of regular expressions in URI template variables. The syntax is {varName:regex} where the first part defines the variable name and the second - the regular expression."

这是他们(相当复杂)的示例:

Here is their (fairly complex) example:

@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{extension:\\.[a-z]+}")
public void handle(@PathVariable String version, @PathVariable String extension) {
    // ...
}

来源: http://docs. spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

他们提供的示例显示了如何执行从请求到控制器方法参数的复杂映射,而如果不使用正则表达式则无法实现.

The example they provide shows how you perform complex mappings from requests to controller method paramters that you wouldn't be able to without using a regular expression.

这篇关于Spring MVC requestmapping处理程序方法中{id:.+}的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆