Java Rest Jersey : Posting multiple types of data (File and JSON)

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...

@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?

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.

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.

Then you can just use @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

Other rreading:


UPDATE

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jersey REST Client : Posting MultiPart data

From Dev

Returning file/files in JSON response (Java-Jersey-ReST)

From Dev

Send and receive JSON to REST WebService in Jersey Java

From Dev

Jersey FormData, upload multiple file data

From Dev

Jersey FormData, upload multiple file data

From Dev

REST api with C#.NET complex json data in body posting

From Dev

REST endpoint returning multiple JSON types

From Java

Posting a File and Associated Data to a RESTful WebService preferably as JSON

From Dev

Java Array with multiple data types

From Dev

How to get plain JSON data in jersey REST implementation?

From Dev

How to get plain JSON data in jersey REST implementation?

From Dev

How to parse JSON array using Jersey Rest Webservices and Java

From Dev

Jquery posting JSON to local file

From Dev

Posting FormData object with input types and file NullPointerException

From Dev

Trouble in posting data as form data in Angular JS to Java REST web-service

From Dev

Posting data with json in xcode 6.0.1

From Dev

JSON formatted data posting to an endpoint

From Java

Bad request with response code 400, when posting json data to rest api using okHttp 4.*

From Dev

Bad request with response code 400, when posting json data to rest api using okHttp 4.*

From Dev

Spring REST API for posting jar file

From Dev

Java File Scanning multiple lines and variable types

From Dev

Error posting json parameter to REST service

From Dev

Java async posting data structure

From Dev

POSTing to a collection association using Spring Data Rest

From Dev

Java Scanner try catch multiple data types

From Dev

Java REST jersey produces URL

From Dev

Jersey - Json to java string

From Dev

Posting to REST api using Java, specifically Android

From Dev

Posting an (Image-) URL of a file from Fileserver and Associated Data as JSON via HTTP

Related Related

  1. 1

    Jersey REST Client : Posting MultiPart data

  2. 2

    Returning file/files in JSON response (Java-Jersey-ReST)

  3. 3

    Send and receive JSON to REST WebService in Jersey Java

  4. 4

    Jersey FormData, upload multiple file data

  5. 5

    Jersey FormData, upload multiple file data

  6. 6

    REST api with C#.NET complex json data in body posting

  7. 7

    REST endpoint returning multiple JSON types

  8. 8

    Posting a File and Associated Data to a RESTful WebService preferably as JSON

  9. 9

    Java Array with multiple data types

  10. 10

    How to get plain JSON data in jersey REST implementation?

  11. 11

    How to get plain JSON data in jersey REST implementation?

  12. 12

    How to parse JSON array using Jersey Rest Webservices and Java

  13. 13

    Jquery posting JSON to local file

  14. 14

    Posting FormData object with input types and file NullPointerException

  15. 15

    Trouble in posting data as form data in Angular JS to Java REST web-service

  16. 16

    Posting data with json in xcode 6.0.1

  17. 17

    JSON formatted data posting to an endpoint

  18. 18

    Bad request with response code 400, when posting json data to rest api using okHttp 4.*

  19. 19

    Bad request with response code 400, when posting json data to rest api using okHttp 4.*

  20. 20

    Spring REST API for posting jar file

  21. 21

    Java File Scanning multiple lines and variable types

  22. 22

    Error posting json parameter to REST service

  23. 23

    Java async posting data structure

  24. 24

    POSTing to a collection association using Spring Data Rest

  25. 25

    Java Scanner try catch multiple data types

  26. 26

    Java REST jersey produces URL

  27. 27

    Jersey - Json to java string

  28. 28

    Posting to REST api using Java, specifically Android

  29. 29

    Posting an (Image-) URL of a file from Fileserver and Associated Data as JSON via HTTP

HotTag

Archive