ITPub博客

首页 > 应用开发 > Java > Map<String, Object>转换成uri请求串

Map<String, Object>转换成uri请求串

原创 Java 作者:20170405 时间:2020-08-28 15:06:12 0 删除 编辑

  将Map转换成uri请求串,主要是如果Object是String[]的话,原先的就构造失败了

  /**

  * 生成get参数请求url

  * 示例:https://0.0.0.0:80/api?u=u&o=o

  * 示例:https://0.0.0.0:80/api

  *

  * @param protocol 请求协议 示例: http 或者 https

  * @param uri 请求的uri 示例: 0.0.0.0:80

  * @param params 请求参数

  * @return

  */

  原版的 (我忘了从哪里复制的了)

  public String generateRequestParameters(String protocol, String uri, Mapparams) {

  StringBuilder sb = new StringBuilder(protocol).append("://").append(uri);

  if (!params.isEmpty()) {

  sb.append("?");

  for (Map.Entry map : params.entrySet()) {

  sb.append(map.getKey())

  .append("=")

  .append(map.getValue())

  .append("&");

  }

  uri = sb.substring(0, sb.length() - 1);

  return uri;

  }

  return sb.toString();

  }

  我优化的

  为了兼容

  public String generateRequestParameters(String protocol, String uri, Mapparams) {

  StringBuilder sb = new StringBuilder(protocol).append("://").append(uri);

  if (!params.isEmpty()) {

  sb.append("?");

  for (Map.Entry map : params.entrySet()) {   郑州祛胎记治疗价钱 http://www.zykdbhk.com/

  String t_key = (String) map.getKey();

  Object t_oValue = map.getValue();

  String t_strValue = "null";

  if (t_oValue instanceof String)

  {

  t_strValue = (String) t_oValue;

  }

  else if (t_oValue instanceof String[])

  {

  t_strValue = StringUtils.join((String[]) map.getValue());

  }

  else

  {//默认

  t_strValue = t_oValue.toString();

  }

  sb.append(map.getKey())

  .append("=")

  .append(t_strValue)

  .append("&");

  }

  uri = sb.substring(0, sb.length() - 1);

  return uri;

  }

  return sb.toString();

  }

  这个Map的来源是

  //获取打标签的用户列表

  @RequestMapping(value = "/enterttt/mm/gg/tt",method = RequestMethod.GET)

  public String getTagUserList(Model model, HttpServletRequest request) {

  String t_path = request.getContextPath();

  String t_path2 = request.getServletPath();

  String t_path3 = request.getRequestURI();

  //获取所有参数

  Map map = request.getParameterMap();

  MapuriMap = new HashMap<>(16);

  uriMap.putAll(map);


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69979119/viewspace-2715526/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇:  集合
请登录后发表评论 登录
全部评论

注册时间:2020-07-14

  • 博文量
    63
  • 访问量
    39785