使用REST客户端调用多部分/表单数据Rest Web服务 [英] Use REST client to call multipart/form-data Rest web service
问题描述
我有一个基于RESTeasy的REST Web服务(请参见下文).我正在尝试使用Google REST客户端执行请求以测试我的服务,但不确定如何设置请求.
I have a RESTeasy-based REST web service (see below). I'm trying to use the google REST client to execute a request to test my service, but I'm unsure as to how the request should be setup.
我不确定如何将byte[]
作为参数(filedata
)发送.
关于如何测试的任何想法?
I'm not sure how to send the byte[]
as a param (filedata
).
Any ideas on how to test this?
我得到以下异常:
java.io.IOException:无法获得多部分边界
java.io.IOException: Unable to get boundary for multipart
使用
request:
-content-type=multipart/form-data
-form params:
test=testvalue
其余方法:
@POST
@Path("/upload")
@Consumes("multipart/form-data")
public Response create(@MultipartForm FileUploadForm form) {
System.out.println("form=" + form.getTest());
return null;
}
FileUploadForm Pojo:
FileUploadForm Pojo:
import javax.ws.rs.FormParam;
import org.jboss.resteasy.annotations.providers.multipart.PartType;
public class FileUploadForm {
private byte[] filedata;
private String test;
public FileUploadForm() {}
public byte[] getFileData() {
return filedata;
}
@FormParam("filedata")
@PartType("application/octet-stream")
public void setFileData(final byte[] filedata) {
this.filedata = filedata;
}
public String getTest() {
return test;
}
@FormParam("test")
@PartType("application/json")
public void setTest(String test) {
this.test = test;
}
}
推荐答案
您需要将此标头添加到您的请求中:
You need to add this header to your request:
Accept-Encoding:multipart/form-data
通常,您使用以下内容类型:
usually you use Content type like this:
Content-Type: image/png
您可以使用 Postman REST客户端
我已附上一张应如何填写表格的图片.
I've attached an image on how the form should be filled out.
这篇关于使用REST客户端调用多部分/表单数据Rest Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!