title: How to parse and decode URI in Java to URI components?
body: <p>I am trying to find a method that would parse an URL, decoded it and returned the decoded components in an unambiguous way.</p>
  
  <p><a href="http://docs.oracle.com/javase/7/docs/api/java/net/URLDecoder.html" rel="nofollow">URLDecoder</a> isn't a right fit, because it may return ambiguous String, e.g.</p>
  
  <pre><code>URLDecoder.decode("http://www.google.com?q=abc%26def", "UTF-8")
  </code></pre>
  
  <p>returns:</p>
  
  <pre class="lang-none prettyprint-override"><code>http://www.google.com?q=abc&amp;def 
  </code></pre>
  
  <p>So the information about escaped &amp; is lost.</p>
  
  <p>I'd like to have something like:</p>
  
  <pre><code>DecodedUrlComponents cmp = GreatURLDecoder.decode(url);
  Map&lt;String, List&lt;String&gt;&gt; decodedQuery = cmp.getQuery();
  decodedQuery.get("q").get(0); //returns "abc&amp;def"
  </code></pre>
  
  <p>How do I accomplish that?</p>
  
  <p>EDIT:
  Thanks for the responses, but my question was a bit different: I would like to get <strong>decoded</strong> components in an unambiguous way, so neither of the following does what I need:</p>
  
  <ul>
  <li><code>new URI("http://www.google.com?q=abc%26def").getRawQuery()</code> returns encoded query: <code>q=abc%26def</code></li>
  <li><code>new URI("http://www.google.com?q=abc%26def").getQuery()</code> returns ambiguous value: <code>q=abc&amp;def</code></li>
  <li><code>URLDecoder.decode("http://www.google.com?q=abc%26def", "UTF-8")</code> returns ambiguous value: <code>http://www.google.com?q=abc&amp;def</code></li>
  <li><code>org.springframework.web.util.UriComponentsBuilder.fromUriString("http://www.google.com?q=abc%26def").build(true).getQueryParams()</code> - close, but still not what I want, because it returns a map of encoded params: <code>{q=[abc%26def]}</code></li>
  </ul>