20.10. Output with multipart/related
RESTEasy provides a simple API to output
multipart/related
.
package org.jboss.resteasy.plugins.providers.multipart; public class MultipartRelatedOutput extends MultipartOutput { public OutputPart getRootPart() public OutputPart addPart(Object entity, MediaType mediaType, String contentId, String contentTransferEncoding) public String getStartInfo() public void setStartInfo(String startInfo) }
To output
multipart/related
, create a MultipartRelatedOutput
object and call addPart()
methods. The first added part is used as the root part of the multipart/related
message. RESTEasy automatically locates a MessageBodyWriter
to marshal your entity objects. As with MultipartInput
, your marshaling may be sensitive to generic type metadata. In this case, use GenericType
. The example below returns a multipart/related
format to the calling client — a HTML file with two images.
@Path("/related") public class MyService { @GET @Produces("multipart/related") public MultipartRelatedOutput get() { MultipartRelatedOutput output = new MultipartRelatedOutput(); output.setStartInfo("text/html"); Map<String, String> mediaTypeParameters = new LinkedHashMap<String, String>(); mediaTypeParameters.put("charset", "UTF-8"); mediaTypeParameters.put("type", "text/html"); output .addPart( "<html><body>\n" + "This is me: <img src='cid:http://example.org/me.png' />\n" + "<br />This is you: <img src='cid:http://example.org/you.png' />\n" + "</body></html>", new MediaType("text", "html", mediaTypeParameters), "<mymessage.xml@example.org>", "8bit"); output.addPart("// binary octets for me png", new MediaType("image", "png"), "<http://example.org/me.png>", "binary"); output.addPart("// binary octets for you png", new MediaType( "image", "png"), "<http://example.org/you.png>", "binary"); client.putRelated(output); return output; } }