我一般不习惯用Java编程,这是我的第一篇文章,所以我要求一点耐心。我无法将这些部分放在一起,以便通过GET请求然后从服务器接收多部分消息的独立客户端。我应该注意,我无法改变这些消息的格式。
收到的协议标题如下所示:
HTTP/1.1 200 OK
Content-type: multipart/custom; boundary=custom-17940605525230162027
X-Custom-Domain-Name: my-custom-domain
Server: Myserver
Content-Length: 71576
Connection: Keep-Alive
Keep-Alive: timeout=5
收到的正文具有上面指出的自定义边界元素,格式如下(内容大部分已省略):
--custom-17940605525230162027
X-Custom-Designation: ack
Content-Type: application/xml
Content-Length: 291
<cust:ack>
A bunch of xml...
</cust:ack>
--custom-17940605525230162027
X-Custom-Designation: update
Content-Type: application/xml
Content-Length: 1101
<cust:update>
A bunch more xml...
</cust:update>
--custom-17940605525230162027
X-Custom-Designation: document
Content-Type: application/xml
Content-Length: 6259
<?xml version="1.0" encoding="UTF-8"?>
Lots more xml...
--custom-17940605525230162027
...
以下代码将正确地从服务器获取上述内容(请暂时忽略无关的库):
package com.mycompany.multipart.test;
import java.io.IOException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
public class ClientMultipartTest {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("user1", "password1");
WebTarget webTarget = client.target("http://theserver:80/test.xqy")
.queryParam("domain", "12345")
.queryParam("target", "67890");
webTarget.register(feature);
Invocation.Builder invocationBuilder = webTarget.request("multipart/mixed");
Response response = invocationBuilder.get();
System.out.println(response.getStatus());
System.out.println(response.readEntity(String.class));
}
}
但我无法弄清楚如何将实体作为多部分对象拉出来。当我尝试以下代码(在webTarget.register()
之后开始)时:
MultiPart response = webTarget.request("multipart/mixed").get(MultiPart.class);
我得到以下异常:
Dec 22, 2014 10:56:15 PM org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor aroundReadFrom
SEVERE: MessageBodyReader not found for media type=multipart/custom; boundary=custom-17940605525230162027, type=class org.glassfish.jersey.media.multipart.MultiPart, genericType=class org.glassfish.jersey.media.multipart.MultiPart.
Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=multipart/custom; boundary=custom-17940605525230162027, type=class org.glassfish.jersey.media.multipart.MultiPart, genericType=class org.glassfish.jersey.media.multipart.MultiPart.
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:225)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:149)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1124)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:851)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:783)
at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:326)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:761)
at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:90)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:671)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:667)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:396)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:296)
at com.mycompany.multipart.test.ClientMultipartTest.main(ClientMultipartTest.java:29)
我想我明白我需要创建并注册&#34;实现自定义MessageBodyReader接口的类(如here所示)。然而,这个例子让我很困惑。
是否有人能够引导我,以便我能够找到一个可以解析其身体部位的MultiPart对象?我最终还需要能够构建一个仅包含<cust:ack>
元素的多部分响应,并将其发布回服务器。
非常感谢您提供任何可以提供的帮助!
答案 0 :(得分:0)
比以往任何时候都晚。您正在寻找的是这个
MultiPart content = response.readEntity(MultiPart.class);
这将导致Jersey框架解析内容,并为您提供一个MultiPart对象,您可以通过该对象遍历零件。