i need consume request content-type multipart/related. request consist of image, json payload , binary content. tried find example on how handle such request in spring boot application, found references on how handle multipart/form-data request nothing related multipart/related mime type.
request this:
post /upload content-type: multipart/related; boundary="123asdf234" --123asdf234 content-type: application/json; charset=utf-8 content-disposition: form-data { "json": "payload" } —-123asdf234 content-type: application/zip content-disposition: file-data; filename="some.zip"; size=123456; <binary-attachment-content> —-123asdf234 content-type: image/png content-disposition: file-data; filename="image1.png"; size=123456; <binary-attachment-content> —-123asdf234-—
can tell on how handle request in spring boot application. i'm using jaxrs.
to solve first referred http://cxf.apache.org/docs/jax-rs-multiparts.html understand multipart/related in relation jax-rs. next referred spring documentation on jax-rs , chose use jersey dependency solve it. referring jersey documentation build following test project: https://github.com/shawntuatara/stackoverflow-38838926. main example is:
package ca.tuatara.stackoverflow; import javax.ws.rs.consumes; import javax.ws.rs.post; import javax.ws.rs.path; import javax.ws.rs.produces; import javax.ws.rs.core.mediatype; import org.glassfish.jersey.media.multipart.bodypart; import org.glassfish.jersey.media.multipart.formdataparam; import org.glassfish.jersey.media.multipart.multipart; import org.glassfish.jersey.media.multipart.multipartfeature; import org.glassfish.jersey.server.resourceconfig; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.stereotype.component; @springbootapplication public class stackoverflow38838926application { public static void main(string[] args) { springapplication.run(stackoverflow38838926application.class, args); } @component public class jerseyconfig extends resourceconfig { public jerseyconfig() { register(multipartfeature.class); register(multiparthandler.class); register(multipartpartshandler.class); } } @component @path("/upload") @consumes("multipart/*") @produces("text/text") public class multiparthandler { @post public string upload(multipart request) { stringbuffer response = new stringbuffer(); (bodypart part : request.getbodyparts()) { if (mediatype.application_json_type.iscompatible(part.getmediatype())) { response.append(part.getentityas(jsonmodel.class)); } else if (mediatype.application_xml_type.iscompatible(part.getmediatype())) { response.append(part.getentityas(xmlmodel.class)); } response.append(system.lineseparator()); } return response.tostring(); } } @component @path("/uploadparts") @consumes("multipart/*") @produces("text/text") public class multipartpartshandler { @post public string upload(@formdataparam("json") jsonmodel json, @formdataparam("xml") xmlmodel xml) { return json + system.lineseparator() + xml; } } }
the test shows how send multipart requests. have kept in debug logging can see going on wire when test runs.
there couple issues original post payload won't allow parsed properly. content has have newline between headers , content. if don't provide "name" property content-disposition can use first example ("/upload"). if name form-data can use second example ("/uploadparts"). didn't example image or file upload if read jersey multipart page can see straightforward add parameter input on request method.
Comments
Post a Comment