20.11. @MultipartForm and POJOs
If you are familiar with your
multipart/form-data
packets, you can map them to and from a POJO class with the @org.jboss.resteasy.annotations.providers.multipart.MultipartForm
annotation and the @FormParam
JAX-RS annotation. To do so, define a POJO with a default constructor (at least), and annotate its fields, properties, or both, with @FormParam
s. These @FormParam
s must also be annotated with @org.jboss.resteasy.annotations.providers.multipart.PartType
to be output. For example:
public class CustomerProblemForm { @FormData("customer") @PartType("application/xml") private Customer customer; @FormData("problem") @PartType("text/plain") private String problem; public Customer getCustomer() { return customer; } public void setCustomer(Customer cust) { this.customer = cust; } public String getProblem() { return problem; } public void setProblem(String problem) { this.problem = problem; } }
Once you have defined your POJO class, you can use it to represent
multipart/form-data
. The following code sends a CustomerProblemForm
using the RESTEasy client framework:
@Path("portal") public interface CustomerPortal { @Path("issues/{id}") @Consumes("multipart/form-data") @PUT public void putProblem(@MultipartForm CustomerProblemForm, @PathParam("id") int id); } { CustomerPortal portal = ProxyFactory.create(CustomerPortal.class, "http://example.com"); CustomerProblemForm form = new CustomerProblemForm(); form.setCustomer(...); form.setProblem(...); portal.putProblem(form, 333); }
Note that the
@MultipartForm
annotation tells RESTEasy that the object has @FormParam
, and that it should be marshaled from that parameter. You can use the same object to receive multipart
data. Here is an example of the server-side counterpart to the customer portal.
@Path("portal") public class CustomerPortalServer { @Path("issues/{id}) @Consumes("multipart/form-data") @PUT public void putIssue(@MultipartForm CustomerProblemForm, @PathParam("id") int id) { ... write to database... } }