使用Jersey在多部分响应中返回图像和JSON [英] Returning both image and JSON in multipart response with Jersey

查看:130
本文介绍了使用Jersey在多部分响应中返回图像和JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过尝试创建一个Rest服务来学习Jersey,该服务可以从客户端接收图像,处理该图像并返回包含其他信息(例如,有关处理详细信息)的新图像.

I'm learning Jersey by trying to create a Rest service that receives an image from client, processes the image and returns a new image with additional information (i.e., about processing details).

到目前为止,上传正常.我现在关心的是创建响应.我正在考虑创建一个包含1个bodypart中包含新图像的多部分响应,同时将JSON字符串(包含附加信息)添加到另一个body部分中.但是我没有成功.代码如下:

So far, the uploading works fine. I'm now concerned with creating the response. I'm thinking of creating a multipart response that contains the new image in 1 bodypart while adding a JSON string (that contains the additional info) into another body part. However I wasn't successful. The code is as follows:

        File image = process(oldImage);
        Info info = getInfo();
        String jsonStr = toJson(info);

        MimeMultipart multiPart = new MimeMultipart();

        MimeBodyPart imagePart = new MimeBodyPart();
        imagePart.setContent(Files.readAllBytes(image.toPath()), MediaType.APPLICATION_OCTET_STREAM);

        MimeBodyPart jsonPart = new MimeBodyPart();
        jsonPart.setContent(jsonStr, MediaType.APPLICATION_JSON);

        multiPart.addBodyPart(imagePart);
        multiPart.addBodyPart(jsonPart);
        return Response.ok(multiPart, "multipart/mixed").build();

我收到如下错误消息:

MessageBodyWriter not found for media type=multipart/mixed, type=class com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart, genericType=class com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart.

我已经搜索了一段时间,但是仍然没有找到解决方法. 太好了,您可以帮助指出代码有什么问题,以及应该采用哪种好的方法来解决此问题.

I've been searching for a while however haven't found anyway to fix it. It would be great of you can help pointing out what's wrong with the code and what should be a good approach to take regarding this issue.

推荐答案

我认为com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart不是您想要的类.

I think com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart is not the class you want.

对于Jersey 2.x,请使用MultiPart ="nofollow"> org.glassfish.jersey.media.multipart 包.

For Jersey 2.x, use the MultiPart class from the org.glassfish.jersey.media.multipart package.

要使用多重功能,您需要添加 jersey-media-multipart 模块添加到您的pom.xml文件:

To use multipart features you need to add jersey-media-multipart module to your pom.xml file:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.22.2</version>
</dependency>

并且不要忘记注册 MultiPartFeature :

And don't forget registering the MultiPartFeature:

final Application application = new ResourceConfig()
    .packages("org.glassfish.jersey.examples.multipart")
    .register(MultiPartFeature.class)

在Jersey GitHub 存储库上有一个分段请求的示例.

There's an example of multipart request on Jersey GitHub repository.

有关更多详细信息,请查看Jersey 2.x 文档.

For more details, have a look at Jersey 2.x documentation.

对于旧版Jersey 1.x,您可以使用 MultiPart 类/jersey/multipart/package-summary.html"rel =" nofollow> com.sun.jersey.multipart 包.

For the old Jersey 1.x, you can use the MultiPart class from the com.sun.jersey.multipart package.

jersey-multipart 依赖是必要的:

<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>1.19.1</version>
</dependency>

这篇关于使用Jersey在多部分响应中返回图像和JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆