1

我正在尝试测试 RESTful 服务,该服务配置为使用soapui 工具使用多部分/混合。

@POST
@Path("requestXmlAndAttachment")
@Consumes({ "multipart/mixed" })
@Produces({ MediaType.APPLICATION_XML })
public ResponseTO processRequestXmlAndAttachment(final       com.sun.jersey.multipart.MultiPart     multiPartRequest) {
 // application logic

}

在 SOAPUI 中,我设置了 REST 资源并添加了 2 个参数,第一个文件指向请求 xml,第二个文件包含数据(我从下拉列表中选择了 PLAIN 样式)。

File1       file:request.xml
File2       file:datafile.xls

在 SOAPUI 请求编辑器窗口中,我选择了 multipart/mixed 作为媒体类型。当我向服务器提交请求时,我没有看到文件被附加到请求中。以下是可以在服务器日志中查看的内容:

Feb 7, 2013 1:36:58 PM com.sun.jersey.api.container.filter.LoggingFilter filter
INFO: 56 * Server in-bound request
56> POST http://localhost:7001/myservice/requestXmlAndAttachment
56 > Accept-Encoding: gzip,deflate
56 > Content-Type: multipart/mixed
56 > Content-Length: 0
56 > Host: localhost:7001
56 > Connection: Keep-Alive
56 > User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
56 >

如您所见,Content-Length 为 0。
我已经能够从 java junit 程序测试此服务。这是程序的一部分:

BodyPart bp1 = new BodyPart();
bp1.setEntity(requestXMLString);
bp1.setMediaType(MediaType.APPLICATION_XML_TYPE);

BodyPart bp2 = new BodyPart();
bp2.setContentDisposition(ContentDisposition.type("file").fileName("datafaile.xls").build  ());
bp2.setEntity(binaryData1);
bp2.setMediaType(MediaType.APPLICATION_OCTET_STREAM_TYPE);


MultiPart multiPart = new MultiPart().bodyPart(bp1).bodyPart(bp2);

ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(MultiPartWriter.class);
Client client = Client.create(cc);
WebResource webResource = client.resource ("http://localhost:7001/myservice/requestXmlAndAttachment");
ClientResponse response = webResource.type(new MediaType("multipart", "mixed")).post(ClientResponse.class, multiPart);

我还尝试在 SOAP UI 请求编辑器框中将媒体类型更改为 multipart/formdata(只是为了试验),然后我可以看到 Content-Length = 30654 并且我看到在服务器日志上读取的文件内容。

我还尝试在请求中添加 http 标头(MIME-Version 等),但没有任何区别。

虽然我可以在没有 SOAPUI 的情况下进行单元测试,但我更愿意让这个测试与 SOAPUI 一起工作,因为我已经在 soapui 中设置了许多其他测试,在同一服务中执行不同的方法。

无论如何使用 SOAP UI 来测试多部分/混合附件?
提前感谢您的调查。

4

0 回答 0