How to automatically map multipart/form-data input to a bean in Jersey

狂风中的少年 提交于 2019-12-12 04:23:22

问题


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) {}


来源:https://stackoverflow.com/questions/42251290/how-to-automatically-map-multipart-form-data-input-to-a-bean-in-jersey

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!