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

后端 未结 1 401
庸人自扰
庸人自扰 2020-12-15 13:23

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 JSO

1条回答
  •  被撕碎了的回忆
    2020-12-15 13:39

    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:

    
        org.glassfish.jersey.media
        jersey-media-multipart
        ${jersey.version}
    
    

    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:

    • 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 this artifact

    
        com.sun.jersey.contribs
        jersey-multipart
        ${jersey.version}
    
    

    0 讨论(0)
提交回复
热议问题