如何从HttpURLConnection创建ResponseEntity?

时间:2016-08-03 15:10:32

标签: java spring rest controller

我想将HttpURLConnection的响应检索到ResponseEntity中。我收到了406错误。

提前谢谢。

@RequestMapping(value = "/retrive", method = { RequestMethod.GET })
    public ResponseEntity<?> 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 && headerValue != null){
            responseHeaders.set(headerName, headerValue);
          }
          if (headerName == null && headerValue == null) {
            break;
          }
        }
        //3. create ResponseEntity
        InputStream inputStream = uc.getInputStream();
        return new ResponseEntity<>(inputStream, responseHeaders, HttpStatus.OK);
    }

2 个答案:

答案 0 :(得分:1)

假设您正在尝试构建某种反向代理,那么您只需从输入流复制到输出流:

  @RequestMapping(value = "/retrive", method = { RequestMethod.GET })
  public ResponseEntity<?> retrive (HttpServletRequest request, HttpServletResponse response){
    //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 && headerValue != null){
        responseHeaders.set(headerName, headerValue);
      }
      if (headerName == null && headerValue == null) {
        break;
      }
    }

    try (InputStream inputStream = uc.getInputStream();
         OutputStream outputStream = response.getOutputStream();
        )
    {
      IOUtils.copy(inputStream, outputStream);
    }
    //3. create ResponseEntity
    return new ResponseEntity<>(responseHeaders, HttpStatus.OK);
  }

答案 1 :(得分:0)

也许,你需要的是将呼叫重定向到另一个网址?

@RequestMapping(path="/retrieve", method = RequestMethod.GET)
    public void stack(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect("http://download.thinkbroadband.com/1GB.zip");
    }

这样,你就不会玩中间人阅读和写作内容。

否则,我也更新了之前的答案,这应该可以避免内存不足错误:

@RequestMapping(value = "/retrieve", method = {RequestMethod.GET})
public void retrieve(HttpServletRequest request, HttpServletResponse response) throws IOException {
    URL u = new URL("http://localhost:8080/getHello");
    OutputStream outputStream = response.getOutputStream();

    try (InputStream imageStream = u.openStream()) {
        IOUtils.copy(imageStream, outputStream );
    }
}

IOUtils.copy基本上以块的形式读/写输入,而不是一步读取整个响应。

你需要一个IOUtils的公共字段:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>