How to automatically map multipart/form-data input

2019-09-06 06:50发布

问题:

I have a Jersey REST api that receives inputs as multipart/form-data. The signature is as follows:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/getorders")
public Response getOrders(final FormDataMultiPart request) {

The input parameters in the form are:

clientName
orderType
year

I would like instead to have something like this:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/getOrders")
public Response getOrders(final OrderBean order) {

And get all my inputs in a bean like this:

public class OrderBean {

    private String clientName;
    private int orderType;
    private int year;

    // Getters and setters
}

Is there a way to do that automatically with Jersey? I know that I can map the fields manually and fill in the bean, but actually I'm looking for an annotation or something like that, that can fill in the bean automatically.

回答1:

Jersey supports @FormDataParams in a @BeanParam bean. If you were to do this (as you would see in most examples):

@POST
public Response post(@FormDataParam("clientName") String clientName) {}

Then you can also do

class OrderBean {
  @FormDataParam("clientName")
  private String clientName;

  // getter/setters
}

@POST
public Response post(@BeanParam OrderBean order) {}


标签: java rest jersey