Java Rest Jersey:发布多种类型的数据(文件和JSON)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27643558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Rest Jersey : Posting multiple types of data (File and JSON)
提问by AbuMariam
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...
我有一个将发布数据的 Jersey REST 服务。将有一个 CSV 文件,它是该 CSV 的实际数据和一些元数据(元数据可以是 JSON 或 XML 格式)。如果这两个都需要发布,服务的方法签名和伴随注释应该如何,应该是这样的......
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({MediaType.APPLICATION_JSON})
public CreateTaskVO provideService(@FormParam("meta") String v1,
@FormParam("data") InputStream v2) {
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?
在这里,我设想第一个参数是元数据的 JSON 字符串,第二个参数是实际数据的输入流。这行得通吗?
采纳答案by Paul Samsotha
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.
您应该使用一些多部分格式。它基本上由一个类型的消息multipart/xxx
(其中xxx
可以是form-data
)组成,并且该消息由其他“完整”消息组成,它们具有自己的内容类型和其他元数据。
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:
您尚未指定哪个 Jersey 版本,但从 Jersey 2.xx 开始,以单独工件的形式提供了多部分支持:
<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.
然后您只需要注册该功能,如注册 中所示。
Then you can just use @FormDataParam
然后你就可以使用 @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 exampleof how the data can be sent from the client, and also the internal message body format of a multipart
您可以在此处查看如何从客户端发送数据的示例,以及多部分的内部消息正文格式
Other rreading:
其他重读:
- General Information on Jersey Multipart support
- General information on
multipart/form-data
- JAX-RS Post multiple objects
UPDATE
更新
There is also support for multipart in Jersey 1.x.x, in the form of thisartifact
Jersey 1.xx 中也支持 multipart,以这个神器的形式
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>