将多个响应组合到一个响应中

时间:2016-07-26 18:10:51

标签: java jaxb jersey jersey-2.0 jaxb2

我有下面的内容下载文件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;


    }

尝试按如下方式添加一个arraylist:

@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:Java类java.util.ArrayList的消息体编写器和Java类型类java.util.ArrayList,未找到MIME媒体类型application / json

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();

2 个答案:

答案 0 :(得分:0)

尝试:

StringBuffer response = new StringBuffer();
response.append(file1Response );
response.append(file2Response );

return response.toString();

如果不起作用,只会试图提供帮助,不会造成伤害。 但如果它竖起大拇指。

答案 1 :(得分:0)

我建议您创建另一个实际返回字符串校验和的方法,然后让您的资源方法调用它。

@GET
@Path("{file}/checksum")
Response getChecksum(@PathParam("file") final String file) {
    return Response.ok(calculateChecksum(file)).build();
}


@POST
@Path("checksums")
Response getChecksum(final List<String> toChecksum) {
    final List<String> checksums = new ArrayList<>();
    for (String s : toChecksum) {
       checksums.add(calculateCheckSum(s));
    }
    return Response.ok(checksums).build();
}



private String calculateCheckSum(final String file) {
    // do your checksum
}