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

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

Scroll Down

对接渠道经常会用到,将request转成map集合
以前网上找了个很长,这次渠道自带的方法很舒服,就更新了

@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生成签名字符串,根据需求修改

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