使用Jersey聚合参数

时间:2016-04-19 11:47:15

标签: java jersey

我正在研究jersey framework ..对于API,我们发送这样的multiformdata

public Response get(@HeaderParam("usrId") int usrId, @HeaderParam("id") int id, @FormDataParam("workFile") InputStream workFileStream, @FormDataParam("workFile") FormDataContentDisposition workFileDetail, @FormDataParam("workNotes") String workNotes, @FormDataParam("conditionFile") InputStream conditionFileStream,
@FormDataParam("conditionFile") FormDataContentDisposition conditionFileDatails, @FormDataParam("conditionNotes") String conditionNotes, @FormDataParam("DesignConditionFile") InputStream designConditionStream, @FormDataParam("DesignConditionFile") FormDataContentDisposition fileDesignCondition, @FormDataParam("desgnConditionNotes") String desgnConditionNotes,
@FormDataParam("drawingFile") InputStream drawingFileStream, @FormDataParam("drawingFile") FormDataContentDisposition fileDrawing, @FormDataParam("desgnDrawingNotes") String desgnDrawingNotes, @FormDataParam("listMakesFile") InputStream listMakesStream, @FormDataParam("listMakesFile") FormDataContentDisposition fileListMakes, @FormDataParam("listMakesNotes") String listMakesNotes,
@FormDataParam("paymentTermsFile") InputStream paymentTermsStream, @FormDataParam("paymentTermsFile") FormDataContentDisposition filePaymentTerms, @FormDataParam("paymentTermsNotes") String paymentTermsNotes, @FormDataParam("bankGuaranteFile") InputStream bankGuaranteStream, @FormDataParam("bankGuaranteFile") FormDataContentDisposition fileBankGuarante,
@FormDataParam("bankGuarnteeNotes") String bankGuarnteeNotes, @FormDataParam("commPointPrjReq") Boolean commPointPrjReq, @FormDataParam("commPointPrjTimeline") String commPointPrjTimeline, @FormDataParam("prjClauseFile") InputStream prjClauseFileStream, @FormDataParam("prjClauseFile") FormDataContentDisposition filePrjClause, @FormDataParam("prjClause") String prjClauseNotes){

.....code

}

对于一个API我们发送了这么多文件..并且为了定义参数,它会消耗5个文件..

我们可以在单独的输入类(input.java)中定义这个formadata,并像这样调用API中的输入类

public Response get(@HeaderParam("usrId") int usrId, @HeaderParam("id") int id, Input input)
{

............code
}

我们可以像这样定义输入类

public class input
{

@FormDataParam("workFile") InputStream workFileStream, @FormDataParam("workFile") FormDataContentDisposition workFileDetail, @FormDataParam("workNotes") String workNotes, @FormDataParam("conditionFile") InputStream conditionFileStream,
@FormDataParam("conditionFile") FormDataContentDisposition conditionFileDatails, 
@FormDataParam("conditionNotes") String conditionNotes, @FormDataParam("DesignConditionFile") InputStream designConditionStream, @FormDataParam("DesignConditionFile") FormDataContentDisposition fileDesignCondition, 
@FormDataParam("desgnConditionNotes") String desgnConditionNotes,
@FormDataParam("drawingFile") InputStream drawingFileStream, @FormDataParam("drawingFile") FormDataContentDisposition fileDrawing, @FormDataParam("desgnDrawingNotes") String desgnDrawingNotes, @FormDataParam("listMakesFile") InputStream listMakesStream, @FormDataParam("listMakesFile") FormDataContentDisposition fileListMakes, @FormDataParam("listMakesNotes") String listMakesNotes,
@FormDataParam("paymentTermsFile") InputStream paymentTermsStream, @FormDataParam("paymentTermsFile") FormDataContentDisposition filePaymentTerms, 
@FormDataParam("paymentTermsNotes") String paymentTermsNotes, @FormDataParam("bankGuaranteFile") InputStream bankGuaranteStream, @FormDataParam("bankGuaranteFile") FormDataContentDisposition fileBankGuarante,
@FormDataParam("bankGuarnteeNotes") String bankGuarnteeNotes, @FormDataParam("commPointPrjReq") Boolean commPointPrjReq, @FormDataParam("commPointPrjTimeline") String commPointPrjTimeline, @FormDataParam("prjClauseFile") InputStream prjClauseFileStream, @FormDataParam("prjClauseFile") FormDataContentDisposition filePrjClause, @FormDataParam("prjClause") String prjClauseNotes

}

1 个答案:

答案 0 :(得分:2)

尝试使用BeanParam。注释可以用作"参数聚合器"可以注入资源类字段,属性或资源方法参数。它可以用作以下内容:

1。将参数包装到类

创建POJO以汇总参数:

public class Input {

    @FormDataParam("workFile") 
    private InputStream workFileStream;

    @FormDataParam("workFile")
    private FormDataContentDisposition workFileDetail;

    @FormDataParam("workNotes") 
    private String workNotes;

    @FormDataParam("conditionFile") 
    private InputStream conditionFileStream;

    @FormDataParam("conditionFile") 
    private FormDataContentDisposition conditionFileDatails;

    @FormDataParam("conditionNotes")
    private String conditionNotes;

    @FormDataParam("DesignConditionFile")
    private InputStream designConditionStream;

    @FormDataParam("DesignConditionFile")
    private FormDataContentDisposition fileDesignCondition;

    @FormDataParam("desgnConditionNotes")
    private String desgnConditionNotes;

    @FormDataParam("drawingFile")
    private InputStream drawingFileStream;

    @FormDataParam("drawingFile")
    private FormDataContentDisposition fileDrawing;

    @FormDataParam("desgnDrawingNotes")
    private String desgnDrawingNotes;

    @FormDataParam("listMakesFile")
    private InputStream listMakesStream;

    @FormDataParam("listMakesFile")
    private FormDataContentDisposition fileListMakes;

    @FormDataParam("listMakesNotes")
    private String listMakesNotes;

    @FormDataParam("paymentTermsFile")
    private InputStream paymentTermsStream;

    @FormDataParam("paymentTermsFile")
    private FormDataContentDisposition filePaymentTerms;

    @FormDataParam("paymentTermsNotes")
    private String paymentTermsNotes;

    @FormDataParam("bankGuaranteFile")
    private InputStream bankGuaranteStream;

    @FormDataParam("bankGuaranteFile")
    private FormDataContentDisposition fileBankGuarante;

    @FormDataParam("bankGuarnteeNotes")
    private String bankGuarnteeNotes;

    @FormDataParam("commPointPrjReq")
    private Boolean commPointPrjReq;

    @FormDataParam("commPointPrjTimeline")
    private String commPointPrjTimeline;

    @FormDataParam("prjClauseFile")
    private InputStream prjClauseFileStream;

    @FormDataParam("prjClauseFile")
    private FormDataContentDisposition filePrjClause;

    @FormDataParam("prjClause")
    private String prjClauseNotes;

    // Default constructor, getters and setters ommited
}

2。更改资源方法以使用"参数聚合器"类

使用上面定义的POJO作为资源方法的参数:

@POST
public Response upload(@HeaderParam("usrId") int usrId,
                       @HeaderParam("id") int id,
                       @BeanParam Input input) {
    ...
}