spirng 工具封装自己的URL参数接口

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);

}

完毕:

175727_8UHR_3418748.png

使用了 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);

}

 

 

 

 

 

转载于:https://my.oschina.net/u/3418748/blog/1634760

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值