Insight springmvc 根据request-path look up handler method 后缀问题

request-path后缀问题:xxx.action,xxx.json怎样look up 处理xxx 请求的method?

...

Determine handler for the current request. 这个过程描述是这样的:

1.Tries all handler mappings in order. 

2.No choice but to go through all mappings...

3.Checks if any of thepatterns match the given request *原来是这样的*

4.Look up the best-matching handler method for the current request.

详见源码:

private String getMatchingPattern(String pattern, String lookupPath) {
    if (pattern.equals(lookupPath)) {
        return pattern;
    }
    // ...
    boolean hasSuffix = pattern.indexOf('.') != -1;
    if (!hasSuffix && this.pathMatcher.match(pattern + ".*", lookupPath)) {
        return pattern + ".*";
    }
    // ...
    return null;
}

private static List
   
   
    
     asList(String... patterns) {
    return (patterns != null ? Arrays.asList(patterns) : Collections.
    
    
     
     emptyList());
}
    
    
   
   
...

总结:

springmvc 针对没有声明但是有后缀的请求,会进行类正则匹配

原则:不抛弃、不放弃每一个request。

...

需要改进的地方:

每一次不在声明中的request-path,springmvc 都会解析匹配处理。

如果像spring 其他的设计,第一次发现匹配成功,追加到urlmap cache(fix size)性能会更好。

...

设计参考:

对于程序的容错、兼容性处理,可以采用try,best-matching 的设计。

...

附带:

spring 返回json 数据的实现,除了response.getWriter()、@ResponseBody 外,还可以配置MappingJacksonJsonView 来实现。

能匹配到MappingJacksonJsonView,前提是mediaType = application/json。

springmvc 也可以配置根据file extensions 转换 mediaType 的功能。

eg:


   
   
	
    
    
	
    
    
	
    
    
		
     
     
			
      
      
       
       application/xml
      
      
			
      
      
       
       application/json
      
      
		
     
     
	
    
    

   
   

这样,如xxx.json 的请求,默认mediaType = application/json。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值