Springmvc @PathVariable
带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义。
通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过
@PathVariable("xxx") 绑定到操作方法的入参中。
实际业务:调用HTTP接口想统一URL格式
如:
http://www.xxxx.com?a={a}&b={b}
http://www.xxx.com/{a}/{b}
我都想统一调用!
我想要个这样的接口:
/**
* <p>
* 方法说明 : 调用HTTP接口获取数据
* </p>
*
* @action lien6o 2018年2月8日 上午11:17:17 描述
*
* @param url
* 请求路径
* @param sign
* 签名缺省 默认 xxxxx
* @param paraMap
* 参数Map
* @param requestMethod
* HTTP请求方式 基于Spring mvc 注解请求方法的枚举类型 {@link RequestMethod }
* @return String 返回参数
*/
String getDateByHttpAgent(String url, String sign, Map<String, String> paramMap, RequestMethod requestMethod);
其他都好说,就是需要对URL处理
UriTemplateHandler uriTemplateHandler = new DefaultUriTemplateHandler();
// the variables to expand the template
URI expandUrl = uriTemplateHandler.expand(url, paramMap);
源码:
public interface UriTemplateHandler {
/**
* Expand the given URI template from a map of URI variables.
* @param uriTemplate the URI template string
* @param uriVariables the URI variables
* @return the resulting URI
*/
URI expand(String uriTemplate, Map<String, ?> uriVariables);
/**
* Expand the given URI template from an array of URI variables.
* @param uriTemplate the URI template string
* @param uriVariables the URI variable values
* @return the resulting URI
*/
URI expand(String uriTemplate, Object... uriVariables);
}
完毕:
使用了 restTemplate !!
拓展:参考http://www.cnblogs.com/leftthen/p/5212221.html
package org.springframework.util;
public interface PathMatcher {
/**
* 判断传入的path是否可以作为pattern使用
*/
boolean isPattern(String path);
/**
* 使用pattern匹配path
*/
boolean match(String pattern, String path);
/**
* 如名,是否开始部分匹配
*/
boolean matchStart(String pattern, String path);
/**
* 提取path中匹配到的部分,如pattern(myroot/*.html),path(myroot/myfile.html),返回myfile.html
*/
String extractPathWithinPattern(String pattern, String path);
/**
* 提取path中匹配到的部分,只是这边还需跟占位符配对为map,
* 如pattern(/hotels/{hotel}),path(/hotels/1),解析出"hotel"->"1"
*/
Map<String, String> extractUriTemplateVariables(String pattern, String path);
/**
* 提供比较器
*/
Comparator<String> getPatternComparator(String path);
/**
* 合并pattern,pattern1然后pattern2
*/
String combine(String pattern1, String pattern2);
}