获取带有附加数据的流式响应主体

huangapple go评论75阅读模式
英文:

Retrieve streaming response body with additional data

问题

我有一个API,它会做出响应:

  @PostMapping
  public StreamingResponseBody export(<variables>) {
    return outputStream -> method(variables);
  }

包含此API的导出服务并未连接任何数据源 - 它只是一个简单的微服务。 变量主要是:带有附加标志的非常大的JSON文件。

问题是,导出中的方法还会提取额外的数据 List<Object> data,这些数据在进一步处理时是必需的。

如何将 StreamingResponseBodydata 一起传递?
任何其他现成的解决方案也都可以。

英文:

I have an API which responds with:

  @PostMapping
  public StreamingResponseBody export(&lt;variables&gt;) {
    return outputStream -&gt; method(variables);
  }

Export service (which contains this API), does not have any data source attached - it is a simple microservice.
Variables mostly are: really big JSON file with additional flags.

Problem is, that the method at the export also extracts additional data List&lt;Object&gt; data, required for further processing.

How to pass StreamingResponseBody together with data ?
Any other out of the box solution would be great too.

答案1

得分: 0

我已经选择了一个ObjectOutputStream的实现。我已经创建了:

public class Response implements Serializable {
  private byte[] document;
  private List<Object> data;
}

并且方法如下:

public void method(OutputStream os) throws Exception {
   try (ObjectOutputStream oos = new ObjectOutputStream(os)) {
     oos.writeObject(buildObj());
   }
}
英文:

I have went with an ObjectOutputStream implementation. I have created:

public class Response implements Serializable {
  private byte[] document;
  private List&lt;Object&gt; data;
}

and method just:

public void method(OutputStream os) throws Exception {
   try (ObjectOutputStream oos = new ObjectOutputStream(os)) {
     oos.writeObject(buildObj());
   }
}

</details>



huangapple
  • 本文由 发表于 2020年10月24日 15:58:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64511220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定