Java Rest Jersey:发布多种类型的数据(文件和JSON) [英] Java Rest Jersey : Posting multiple types of data (File and JSON)

查看:333
本文介绍了Java Rest Jersey:发布多种类型的数据(文件和JSON)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Jersey REST服务,数据将发布到该服务。将有一个CSV文件,它是该CSV的实际数据和一些元数据(元可以是JSON或XML格式)。如果需要发布这两个方法,方法签名和附加注释应该如何发布,如果它类似......

I have a Jersey REST service to which data will be posted. There will be a a CSV file which is the actual data and some meta-data for that CSV (the meta can either be in JSON or XML format). How should the method signature and accompany annotations for the service look like if both of these need to be posted, should it be something like...

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({MediaType.APPLICATION_JSON})
public CreateTaskVO provideService(@FormParam("meta") String v1,
        @FormParam("data") InputStream v2) {

这里我设想的第一个参数是是元数据的JSON字符串,第二个是实际数据的输入流。这会有用吗?

Here I am envisioning the first parameter to be a JSON string of meta-data and the second an input stream of the actual data. Would this work?

推荐答案

你应该使用一些多部分格式。它基本上由一个类型为 multipart / xxx 的消息组成(其中 xxx 可以类似于 form-data ),该消息由其他完整消息组成,包含自己的内容类型和其他元数据。

You should use some multipart format. It basically consists of a single message of type multipart/xxx (where xxx can be something like form-data), and that message consists of other "complete" messages with their own content-type and other meta data.

你没有指定哪个泽西岛版本,但从泽西2.xx开始,有一个单独的支持可用的多部分支持:

You haven't specified which Jersey version, but starting with Jersey 2.x.x, there is multipart support available, in the form of a separate artifact:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>${jersey.version}</version>
</dependency>

然后你只需要注册这个功能,如图所示在这里注册

Then you just need to register the feature, as seen here in Registration.

然后你可以使用 @FormDataParam

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({MediaType.APPLICATION_JSON})
public CreateTaskVO provideService(
               @FormDataParam("meta") String jsonMeta,
               @FormDataParam("data") InputStream file,
               @FormDataParam("data") FormDataContentDisposition fileDetail) {

你可以看到这里是一个示例,说明如何从客户端发送数据,以及多部分的内部消息体格式

You can see here an example of how the data can be sent from the client, and also the internal message body format of a multipart

其他rreading:

  • General Information on Jersey Multipart support
  • General information on multipart/form-data
  • JAX-RS Post multiple objects

更新

那里还支持Jersey 1.xx中的multipart,以工件的形式

There is also support for multipart in Jersey 1.x.x, in the form of this artifact

<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>${jersey.version}</version>
</dependency>

这篇关于Java Rest Jersey:发布多种类型的数据(文件和JSON)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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