gpt4 book ai didi

java - 使用 Jersey 聚合参数

转载 作者:行者123 更新时间:2023-11-30 02:58:00 25 4
gpt4 key购买 nike

我正在开发 Jersey 框架..对于API,我们正在发送这样的多格式数据

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)中定义这个格式数据并像这样在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

}

最佳答案

尝试使用BeanParam 。该注释可以用作“参数聚合器”,可以注入(inject)到资源类字段、属性或资源方法参数中。它可以按以下方式使用:

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

关于java - 使用 Jersey 聚合参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36717458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com