简体   繁体   English

RESTEasy:在响应中返回InputStream

[英]RESTEasy: return InputStream in response

I'm working on a service to PUT and GET InputStream objects based on a key - these can be anything from basic Strings to files. 我正在基于键为PUT和GET InputStream对象提供服务-这些可以是从基本String到文件的任何东西。 Here is the PUT method which appears to work just fine: 这是看起来很好用的PUT方法:

@PUT
@Path("{key}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response addData(
    final @PathParam("key") String key,
    final InputStream data) {
  final Service service =  new Service();
  try {
    service.addData(key, data);
  } finally {
    IOUtils.closeQuietly(data
  }
  return Response.status(204).build();
}

Now when I want to get the data (based on a key) I want to return the InputStream which is how the object is stored and retrieved. 现在,当我想获取数据(基于键)时,我想返回InputStream,即对象的存储和检索方式。 This is what I have currently: 这是我目前拥有的:

@GET
@Path("/{key}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
  public InputStream getData(
    @PathParam("key") String key) {
  // get service and do other stuff ...
  return service.getData(); //This is an InputStream
}

When I send a GET, I get a status 200 and a 'Response Does Not Contain Any Data' message ... when I examine the request in Fiddler, it doesn't look like the data is attached. 发送GET时,状态为200,并且“响应不包含任何数据”消息……当我在Fiddler中检查请求时,它看起来好像没有附加数据。 Am I missing something simple here? 我在这里缺少简单的东西吗? Thanks. 谢谢。

Edit: Here are the responses from: 编辑:这是来自的回复:

It seems like there's nothing attached to the response, but I'm not sure. 响应似乎没有任何关联,但我不确定。 Is there a good way to test whether or not it's returning the InputStream? 有没有一种很好的方法来测试它是否返回InputStream?


Edit2: Interesting. 编辑2:有趣。 Per peeskillet's suggestion of reading the data to see if it's even there I did this: Per peeskillet建议读取数据以查看是否存在,我这样做是:

final String data = IOUtils.toString(stream);

This returns "" when it should be returning "test" . 这将返回""时,它应该返回"test" Now I'm not super familiar with IOUtils so maybe their toString(InputStream) is causing it to be "test" , but that would suggest that it's not getting set properly in the service. 现在,我对IOUtils不太熟悉,所以也许他们的toString(InputStream)导致它被"test" ,但这表明它在服务中的设置不正确。

My service for getting the InputStream data looks something like this: 我用于获取InputStream数据的服务如下所示:

final InputStream data = getData(_key);
  try {
    if (data == null) {
      return null;
    }
    return object.newBuilder()
      .setKey(_key)
      .setData(data)
      .build();
  } finally {
    IOUtils.closeQuietly(data);
  }

Would anything in the service be causing the stream to be read therefore making it non-accessible? 服务中的任何内容都会导致流被读取,从而使其无法访问吗?


Edit3: The problem is in the service when I set the data to be returned. Edit3:当我设置要返回的数据时,问题出在服务中。 If I remove IOUtils.closeQuietly(data); 如果我删除IOUtils.closeQuietly(data); I'm able to get the data back just fine. 我能够将数据恢复正常。 However this causes issues because I leave an InputStream open... any workaround suggestions? 但是,这会导致问题,因为我将InputStream保持打开状态...有任何解决方法的建议吗?

Returning an InputStream should work unless somebody already read from the stream. 除非有人已经从流中读取,否则返回InputStream应该可以工作。 You can't read twice from an InputStream . 您不能从InputStream读取两次。

The problem was related to leaving the InputStream open. 问题与打开InputStream有关。 However, there was a bug in the version of RestEasy that caused the InputStream to not be closed. 但是,RestEasy版本中存在一个错误,该错误导致InputStream无法关闭。 The ticket/fix is found here: https://issues.jboss.org/browse/RESTEASY-741 票证/修复程序可以在这里找到: https : //issues.jboss.org/browse/RESTEASY-741

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM