是关于一个护理人员的请求。我的应用程序的用户应该能够为请求选择参数。然后,我将从响应中读取xml并使用JAXB解析它。我成功地做到了这一点:
String uri = "https://cmi.zb.uzh.ch/primo/api/oaipmh?verb=ListRecords&metadataPrefix=oai_primo&set=PRIMO&from=2020-01-22T12:39:59Z&until=2020-02-28T11:40:00Z";
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
InputStream xml = connection.getInputStream();
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
oai = (OAIPMHtype) jaxbUnmarshaller.unmarshal(xml);但是我想动态地创建请求。有一个JSF页面,用户可以在其中输入参数。我想到了泽西客户端,因为我可以轻松地用字段替换queryParam中的String。但我不能把泽西的客户和我的解法器联系起来。
我的想法是将响应转换为输入流,并将其提供给解组器。
这是我试过的。
主要方法
public static void main(String[] args) {
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target("https://cmi.zb.uzh.ch/primo/api/oaipmh");
target.queryParam("verb", "ListRecords")
.queryParam("metadataPrefix", "oai-primo")
.queryParam("set", "PRIMO")
.queryParam("from", "2020-01-22T12:39:59Z")
.queryParam("until", "2020-02-28T11:40:00Z" );
InputStream stream = target.request()
.accept(MediaType.TEXT_XML)
.get(InputStream.class);
}
}错误消息
Exception in thread "main" javax.ws.rs.client.ResponseProcessingException: javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type text/xml;charset=utf-8 and type class java.io.InputStream
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:163)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:467)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.get(ClientInvocationBuilder.java:197)
at ch.hbu.sacker.oaipmh.VogellaExample.main(VogellaExample.java:45)
Caused by: javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type text/xml;charset=utf-8 and type class java.io.InputStream
at org.jboss.resteasy.core.interception.ClientReaderInterceptorContext.throwReaderNotFound(ClientReaderInterceptorContext.java:37)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.getReader(AbstractReaderInterceptorContext.java:80)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:53)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:211)
at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:88)
at org.jboss.resteasy.specimpl.AbstractBuiltResponse.readEntity(AbstractBuiltResponse.java:256)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:127)
... 3 more有人知道为什么会这样吗?
发布于 2021-02-07 10:27:51
问题是上面代码的WebTarget --请求只调用了这个https://cmi.zb.uzh.ch/primo/api/oaipmh?verb=ListRecords --这确实会导致一个错误!
<OAI-PMH>
<responseDate>2021-02-07T10:06:28Z</responseDate>
<request verb="ListRecords">https://cmi.zb.uzh.ch/primo/api/oaipmh</request>
<error code="badArgument">Unknown arguments: </error>
</OAI-PMH>正如这里所解释的,泽西岛rest客户端不添加查询参数我不得不覆盖目标。
WebTarget target = client.target("https://cmi.zb.uzh.ch/primo/api/oaipmh?verb=ListRecords");
target = target.queryParam("metadataPrefix", "oai_primo")
.queryParam("set", "PRIMO")
.queryParam("from", "2020-01-22T12:39:59Z")
.queryParam("until", "2020-02-28T11:40:00Z" );发布于 2021-01-27 09:43:43
您正确地尝试使用3d派对Http客户端,而不是使用HttpURLConnection类。您可以做的是尝试其他可用的HTTP客户端。最受欢迎的是Apache Http客户端或好的Http客户端。然而,我也可能建议我自己的开放源码MgntUtils库使用Http客户端。它可能不像其他库那么广泛,但是非常简单,可以多次将请求发送到同一个URL。如果您想使用我的库,您的代码将如下所示
HttpClient client = new HttpClient();
client.setConnectionUrl("https://cmi.zb.uzh.ch/primo/api/oaipmh?verb=ListRecords&metadataPrefix=oai_primo&set=PRIMO&from=2020-01-22T12:39:59Z&until=2020-02-28T11:40:00Z");
client.setRequestProperty("Accept", "application/xml");
String xmlStr = client.sendHttpRequest(HttpMethod.GET);下面是一个用于HttpClient类的Javadoc。这个库可以在Maven伪像或Github上获得(使用源代码和Javadoc)。
https://stackoverflow.com/questions/65909883
复制相似问题