Springmvc @PathVariable html
带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程当中具备里程碑的意义。java
经过 @PathVariable 能够将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符能够经过
@PathVariable("xxx") 绑定到操做方法的入参中。spring
实际业务:调用HTTP接口想统一URL格式mvc
如:url
http://www.xxxx.com?a={a}&b={b}rest
http://www.xxx.com/{a}/{b}code
我都想统一调用!htm
我想要个这样的接口:blog
/** * <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); }