本文介绍了如何自动将multipart/form-data输入映射到Jersey中的Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Jersey REST api,它接收输入作为multipart/form-data.签名如下:

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

表格中的输入参数为:

clientName
orderType
year

我想改成这样:

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

将我所有的输入都放在一个像这样的bean中:

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
}

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

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.

推荐答案

Jersey在@BeanParam bean中支持@FormDataParam.如果您要这样做(在大多数示例中都会看到):

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

那你也可以做

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

  // getter/setters
}

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

这篇关于如何自动将multipart/form-data输入映射到Jersey中的Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 13:46