How to convert map to url query string?

前端 未结 17 1536
盖世英雄少女心
盖世英雄少女心 2020-11-27 13:39

Do you know of any utility class/library, that can convert Map into URL-friendly query string?

Example:

I have a map:

\"param1\"=12,
\"param2         


        
17条回答
  •  春和景丽
    2020-11-27 14:35

    This is the solution I implemented, using Java 8 and org.apache.http.client.URLEncodedUtils. It maps the entries of the map into a list of BasicNameValuePair and then uses Apache's URLEncodedUtils to turn that into a query string.

    List nameValuePairs = params.entrySet().stream()
       .map(entry -> new BasicNameValuePair(entry.getKey(), entry.getValue()))
       .collect(Collectors.toList());
    
    URLEncodedUtils.format(nameValuePairs, Charset.forName("UTF-8"));
    

提交回复
热议问题