在 Java 中使用 Supplier<InputStream> 流给出 Jersey 响应
Give a Jersey response using a Stream of Supplier<InputStream> in Java
所以我现在的处境很糟糕。我有一个包含方法 "getAllData" 的 class 修订版。该方法 return 是 InputStream 的供应商。
public class Revision {
private String id;
private Supplier<InputStream> allData;
public Revision(String id, Supplier<InputStream> allData) {
this.id = id;
this.allData = allData;
}
public getAllData() {
return allData;
}
}
假设我有 2 次修订。它们是这样声明的:
Supplier<InputStream> revisionValue1 = () -> new ByteArrayInputStream(
"{\"name\":\"George\", \"state\":\"Colorado\", \"Exp\":\"lots\"}".getBytes());
Revision revision1 = new Revision("Id1", revisionValue1);
Supplier<InputStream> revisionValue2 = () -> new ByteArrayInputStream(
"{\"name\":\"Sean\", \"state\":\"New York\"}".getBytes());
Revision revision2 = new Revision("Id2", revisionValue2);
然后将它们放入地图成员中:
Map<String, Revision> revisions = new HashMap<>();
revisions.put("Id1", revision1);
revisions.put("Id2", revision2);
一个当前的方法(它本身可能写得不是很好)return一个流:
public Stream<Revision> getRevisions() {
return revisions.values().stream();
}
现在在我的 Jersey 代码中,我需要弄清楚如何 return 这些值作为 Json 格式的响应。所以结果应该类似于:
[
{
"name": "George",
"state": "Colorado",
"Exp": "lots"
},
{
"name": "Sean",
"state": "New York"
}
]
我正在尝试以纯文本格式执行此操作,但无法正常工作。我正在尝试的是:
@Produces(MediaType.TEXT_PLAIN)
public Response getRevisions(...) {
Stream<Revision> revisionObjects = getRevisions();
// each Revision option has a getAllData() method that needs called, which returns Supplier<InputStream>
// each Supplier<InputStream> contains, essentially, a Json record
// attempt at creating a StreamingOutput that can be sent as a response
if (revisionObjects != null) {
StreamingOutput stream = os -> {
Writer writer = new OutputStreamWriter(os);
Iterator<Revision> revisionIterator = revisionObjects.iterator();
while (revisionIterator.hasNext()) {
Revision next = revisionIterator.next();
writer.write(next.getAllData().get().toString());
}
writer.flush();
};
return Response.ok(stream).build();
}
return Response.noContent().build();
}
这是 return 结果如下:
java.io.ByteArrayInputStream@7de636java.io.ByteArrayInputStream@ba0b09
我假设这是因为在作者的 "toString()" 方法之前需要一个额外的步骤。
抱歉,这有多长,我只是想提供详细信息。有谁知道如何让它工作?理想情况下,我希望它以 JSON 响应,但即使是纯文本也会有很大的进步。
P.S。我仍在努力思考 InputStreams 和 OutputStreams,所以很可能,我有一些不合逻辑的东西。
@Produces(MediaType.TEXT_PLAIN)
public Response getRevisions(...) {
Stream<Revision> revisionObjects = getRevisions();
// each Revision option has a getAllData() method that needs called, which returns Supplier<InputStream>
// each Supplier<InputStream> contains, essentially, a Json record
// attempt at creating a StreamingOutput that can be sent as a response
if (revisionObjects != null) {
Iterator<Revision> revisionIterator = revisionObjects.iterator();
List<JSONObject> revisionsResponse = new ArrayList<JSONObject>();
while (revisionIterator.hasNext()) {
BufferedReader streamReader = new BufferedReader(new InputStreamReader(revisionIterator.next().getAllData().get(), "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null)
responseStrBuilder.append(inputStr);
revisionsResponse.add(new JSONObject(responseStrBuilder.toString());
}
return Response.ok(revisionsResponse).build();
}
return Response.noContent().build();
}
所以我现在的处境很糟糕。我有一个包含方法 "getAllData" 的 class 修订版。该方法 return 是 InputStream 的供应商。
public class Revision {
private String id;
private Supplier<InputStream> allData;
public Revision(String id, Supplier<InputStream> allData) {
this.id = id;
this.allData = allData;
}
public getAllData() {
return allData;
}
}
假设我有 2 次修订。它们是这样声明的:
Supplier<InputStream> revisionValue1 = () -> new ByteArrayInputStream(
"{\"name\":\"George\", \"state\":\"Colorado\", \"Exp\":\"lots\"}".getBytes());
Revision revision1 = new Revision("Id1", revisionValue1);
Supplier<InputStream> revisionValue2 = () -> new ByteArrayInputStream(
"{\"name\":\"Sean\", \"state\":\"New York\"}".getBytes());
Revision revision2 = new Revision("Id2", revisionValue2);
然后将它们放入地图成员中:
Map<String, Revision> revisions = new HashMap<>();
revisions.put("Id1", revision1);
revisions.put("Id2", revision2);
一个当前的方法(它本身可能写得不是很好)return一个流:
public Stream<Revision> getRevisions() {
return revisions.values().stream();
}
现在在我的 Jersey 代码中,我需要弄清楚如何 return 这些值作为 Json 格式的响应。所以结果应该类似于:
[
{
"name": "George",
"state": "Colorado",
"Exp": "lots"
},
{
"name": "Sean",
"state": "New York"
}
]
我正在尝试以纯文本格式执行此操作,但无法正常工作。我正在尝试的是:
@Produces(MediaType.TEXT_PLAIN)
public Response getRevisions(...) {
Stream<Revision> revisionObjects = getRevisions();
// each Revision option has a getAllData() method that needs called, which returns Supplier<InputStream>
// each Supplier<InputStream> contains, essentially, a Json record
// attempt at creating a StreamingOutput that can be sent as a response
if (revisionObjects != null) {
StreamingOutput stream = os -> {
Writer writer = new OutputStreamWriter(os);
Iterator<Revision> revisionIterator = revisionObjects.iterator();
while (revisionIterator.hasNext()) {
Revision next = revisionIterator.next();
writer.write(next.getAllData().get().toString());
}
writer.flush();
};
return Response.ok(stream).build();
}
return Response.noContent().build();
}
这是 return 结果如下:
java.io.ByteArrayInputStream@7de636java.io.ByteArrayInputStream@ba0b09
我假设这是因为在作者的 "toString()" 方法之前需要一个额外的步骤。
抱歉,这有多长,我只是想提供详细信息。有谁知道如何让它工作?理想情况下,我希望它以 JSON 响应,但即使是纯文本也会有很大的进步。
P.S。我仍在努力思考 InputStreams 和 OutputStreams,所以很可能,我有一些不合逻辑的东西。
@Produces(MediaType.TEXT_PLAIN)
public Response getRevisions(...) {
Stream<Revision> revisionObjects = getRevisions();
// each Revision option has a getAllData() method that needs called, which returns Supplier<InputStream>
// each Supplier<InputStream> contains, essentially, a Json record
// attempt at creating a StreamingOutput that can be sent as a response
if (revisionObjects != null) {
Iterator<Revision> revisionIterator = revisionObjects.iterator();
List<JSONObject> revisionsResponse = new ArrayList<JSONObject>();
while (revisionIterator.hasNext()) {
BufferedReader streamReader = new BufferedReader(new InputStreamReader(revisionIterator.next().getAllData().get(), "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null)
responseStrBuilder.append(inputStr);
revisionsResponse.add(new JSONObject(responseStrBuilder.toString());
}
return Response.ok(revisionsResponse).build();
}
return Response.noContent().build();
}