【发布时间】:2016-11-30 12:36:08
【问题描述】:
我有以下下载内容为 c50c4a23307529b59797525c52b2c50c *file1.zip 的文件
现在我想将 file1Response 和 file2Response 合并为 json。有什么帮助吗?
@GET
@Path("/" + getfileschecksum)
@Produces("application/json")
public Response getFilesChecksum() {
String fileid1 = "file1";
String fileid2 = "file2";
Response file1Response = getChecksum(fileid1);
Response file2Response = getChecksum(fileid2);
return file1Response;
}
尝试如下添加数组列表:
@GET
@Path("/" + getfileschecksum)
@Produces("application/json")
public Response getFilesChecksum() {
String fileid1 = "file1";
String fileid2 = "file2";
ArrayList<Response> rp = new ArrayList<Response>();
Response file1Response = getChecksum(fileid1);
Response file2Response = getChecksum(fileid2);
rp.add(file1Response);
rp.add(file2Response);
return Response.ok(rp).build();
}
返回错误 com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type application/json was not found .
file1Response 来自下面,可以在下面更改任何内容以返回字符串。
URL url = new URL(binpath);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
String mt = connection.getContentType();
ResponseBuilder response = Response.ok((Object) is, mt);
response.header("Content-Disposition","attachment; filename=" + binpath.substring(binpath.lastIndexOf('/') + 1, binpath.length()));
return response.build();
【问题讨论】:
-
我假设 Response 是 javax.ws.rs.core.Response 对象。首先这不起作用,因为它是一个接口,您必须向它添加 @JsonTypeInfo 以告诉序列化程序如何序列化它。您需要将 ArrayList 转换为 ArrayList
并将实际的字符串值放在那里而不是 Response 对象。 -
我尝试了 file1Response.getEntity().toString(),但这会返回 weblogic.net.http.KeepAliveStream@458324f2 而不是实际内容。如何从响应中获取字符串?
标签: java jaxb jersey jersey-2.0 jaxb2