简体   繁体   English

如何自动将multipart / form-data输入映射到Jersey中的Bean

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

I have a Jersey REST api that receives inputs as multipart/form-data. 我有一个Jersey REST api,它接收输入作为多部分/表单数据。 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: 然后将我所有的输入放入这样的bean中:

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? 有没有一种方法可以自动使用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. 我知道我可以手动映射字段并填充Bean,但实际上我正在寻找可以自动填充Bean的注释或类似内容。

Jersey supports @FormDataParam s in a @BeanParam bean. Jersey在@BeanParam bean中支持@FormDataParam 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) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM