前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >对接常用的工具方法,request转map,转签名字符串等

对接常用的工具方法,request转map,转签名字符串等

作者头像
深雾
发布2020-07-10 10:30:11
1.5K0
发布2020-07-10 10:30:11
举报
文章被收录于专栏:工具类工具类

对接渠道经常会用到,将request转成map集合

以前网上找了个很长,这次渠道自带的方法很舒服,就更新了

代码语言:javascript
复制
@RequestMapping(value = "/deliver", method = { RequestMethod.POST, RequestMethod.GET })
	public void deliver(HttpServletRequest request, HttpServletResponse response) throws IOException {
		LOGGER.error("deliver request  ip:{},queryString: {},parameter:{}", ServletUtils.getRemoteAddress(request), request.getQueryString(),
				request.getParameterMap());
		String result = "FAILURE";
		try {
			Map<String, Object> params = getParameterMap(request);
			result = xingfeiService.doDeliver(params);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		response.getWriter().write(result);
	}

	/**
     * 将请求参数转化为Map
     * 
     * @param request
     * @return
     */
    public static Map<String, Object> getParameterMap(HttpServletRequest request) {
        Map<String, Object> param = new HashMap<>();
        try {
            Enumeration<String> em = request.getParameterNames();
            while (em.hasMoreElements()) {
                String key = em.nextElement();
                param.put(key, request.getParameter(key));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return param;
    }

去除sign生成签名字符串,根据需求修改

代码语言:javascript
复制
public static String signstr(Map<String, Object> params) throws UnsupportedEncodingException {
        if (params == null) {
            return null;
        }
        if (params.get("sign") != null) {
            params.remove("sign");
        }
        List<String> keyList = new ArrayList<>(params.keySet());
        Collections.sort(keyList);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < keyList.size(); i++) {
            String key = keyList.get(i);
            String value = params.get(key).toString();
            sb.append(key + "=" + value + "&");
        }
        String signStr = sb.toString();
        return signStr;
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档