How to create a ResponseEntity from HttpURLConnection?
<p>I would like to retrieve the response from HttpURLConnection  into ResponseEntity. I got the 406 error.</p>

<p>Thank you in advance.</p>

<pre><code>@RequestMapping(value = "/retrive", method = { RequestMethod.GET })
    public ResponseEntity&lt;?&gt; retrive (HttpServletRequest request){
        //1. call a rest by rest client
        URL u = new URL("http://localhost:8080/getHello");
        HttpURLConnection uc = (HttpURLConnection) u.openConnection();

        //2, create HttpHeaders for ResponseEntity
        HttpHeaders responseHeaders = new HttpHeaders();
        for (int i = 0;; i++) {
          String headerName = uc.getHeaderFieldKey(i);
          String headerValue = uc.getHeaderField(i);
          if(headerName != null &amp;&amp; headerValue != null){
            responseHeaders.set(headerName, headerValue);
          }
          if (headerName == null &amp;&amp; headerValue == null) {
            break;
          }
        }
        //3. create ResponseEntity
        InputStream inputStream = uc.getInputStream();
        return new ResponseEntity&lt;&gt;(inputStream, responseHeaders, HttpStatus.OK);
    }
</code></pre>