如何使用jersey客户端从get响应中二进制数据?

时间:2016-04-22 16:11:26

标签: java rest jersey-client

我正在尝试休息使用泽西客户端接听电话。基于api文档,请求将图像作为二进制数据返回。当我完成其余的使用邮递员接听电话时,我可以将实际图像恢复(我asumming postman将二进制文件转换回图像/ png)。这是邮递员返回的以下标题。

enter image description here

我尝试完成其余的在java中使用jersey客户端。这是我的代码:

private Client client = ClientBuilder.newClient( new ClientConfig().register(LoggingFilter.class).register(MultiPartFeature.class));
private WebTarget myServer;

myServer= client.target(baseURL);

 public void restGetImage(String requestURL, String headers) {

    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();

    String line;

    MultivaluedMap<String, Object> userHeaders = storeHeadersInMap(headers);

    WebTarget target = getWebTarget().path(requestURL);

    Response response = target.request(MediaType.APPLICATION_OCTET_STREAM)
                     .headers(userHeaders)
                     .get();

    System.out.println("Reuqest URL: " + session.get("baseurl") + requestURL);
    int responseCode = response.getStatus();
    InputStream inputStream = response.readEntity(InputStream.class);

    String contentType = response.getHeaderString("Content-Type");

 //     br = new BufferedReader(new InputStreamReader(inputStream));
//      
//      try {
  //            while((line = br.readLine()) != null) {
  //                sb.append(line);
 //         }
  //            
 //         br.close(); //close buffered reader 
  //        
 //     } catch (IOException e) {
 //         e.printStackTrace();
//          
//      }
//      
//      RestResponse data = new RestResponse(responseCode, sb.toString(), contentType);
//      data.setResponseHeader(response.getHeaders());
//      
//      System.out.println("response code: " + responseCode);
//      System.out.println("response Content-Type: " + contentType);
//      System.out.println("Response body: " + sb.toString());

//      return data;
    }

请原谅所有评论,因为我仍在测试代码。基本上代码返回200响应状态,但是当它无法读取响应时。它在InputStream inputStream = response.readEntity(InputStrean.class)中抛出异常。

 Apr 22, 2016 11:37:29 AM         
 org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderIn
terceptor aroundReadFrom
SEVERE: MessageBodyReader not found for media type=image/png, type=class   
com.itextpdf.text.pdf.codec.Base64$InputStream, genericType=class   
com.itextpdf.text.pdf.codec.Base64$InputStream.

我的目标是能够读取二进制响应数据。任何洞察力都会被提升。感谢。

1 个答案:

答案 0 :(得分:3)

  

SEVERE:找不到媒体类型= image / png,类型=类
的MessageBodyReader   com.itextpdf.text.pdf.codec.Base64 $ InputStream,genericType = class
  com.itextpdf.text.pdf.codec.Base64 $的InputStream。

您正在使用错误的{- Define an employee type -} data Employee = Engineer {name :: String, engineerId :: String} | Manager {name :: String, managerId :: Integer} | Director {name :: String, directorId :: Integer} deriving Show 课程。您应该使用java.io.InputStream。检查并修复您的导入。