简体   繁体   English

Jersey REST-Client WebTarget - 找不到内容类型的 MessageBodyReader

[英]Jersey REST-Client WebTarget - Unable to find a MessageBodyReader of content-type

It's about a parametrized Request.这是关于一个参数化的请求。 The users of my application should be able to choose the parameters for the request.我的应用程序的用户应该能够为请求选择参数。 I will then read xml from the response and parse it with JAXB.然后,我将从响应中读取 xml 并使用 JAXB 对其进行解析。 I have done it successfully like this:我已经像这样成功地做到了:

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);

But I want to create the request dynamically.但我想动态创建请求。 There is a JSF-Page where the user can enter the parameters.有一个 JSF 页面,用户可以在其中输入参数。 I thought of a Jersey-Client, because I can easily replace the Strings in the queryParam with fields.我想到了 Jersey-Client,因为我可以轻松地将 queryParam 中的字符串替换为字段。 But I cannot link the Jersey-Client to my Unmarshaller.但我无法将 Jersey-Client 链接到我的 Unmarshaller。

My idea is to cast the response into a inputstream and feed it to Unmarshaller.我的想法是将响应转换为输入流并将其提供给 Unmarshaller。

Here is what I tried.这是我尝试过的。

Main Method主要方法

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);

    }
}

Errormessage错误信息

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

Does anyone know why this happens?有谁知道为什么会这样?

You are correct in trying to use 3d party Http client rather than doing it with HttpURLConnection class.您尝试使用 3d 方 Http 客户端而不是使用 HttpURLConnection class 是正确的。 What you can do is try some other HTTP clients available.您可以尝试一些其他可用的 HTTP 客户端。 Popular ones would be Apache Http Client or OK Http client .流行的是Apache Http ClientOK Http client However, I may suggest my own Open Source MgntUtils library with Http client as well.但是,我可能会建议我自己的开源 MgntUtils 库也带有 Http 客户端。 It may be not as extensive as other library but is very simple and geared for sending requests multiple times to the same URL.它可能没有其他库那么广泛,但非常简单,适合向同一个 URL 多次发送请求。 In case you want to use my library your code would look something like this如果您想使用我的库,您的代码将如下所示

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);

Here is a Javadoc for HttpClient class.这是HttpClient class 的 Javadoc。 The library could be obtained as Maven artifacts or on Github (with source code and Javadoc)该库可以作为Maven 工件Github 获得(带有源代码和 Javadoc)

Problem was the WebTarget With the above code the request called only this URL https://cmi.zb.uzh.ch/primo/api/oaipmh?verb=ListRecords which does result in an error!问题是WebTarget使用上面的代码,请求只调用了这个 URL 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>

As explained here Jersey rest client not adding query parameters I had to overwrite target.正如这里所解释的Jersey 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" );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到内容类型为application / octet-stream的MessageBodyReader - Unable to find a MessageBodyReader of content-type application/octet-stream org.jboss.resteasy.client.ClientResponseFailure:无法找到内容类型为文本/纯文本且类型为类java.lang.String的MessageBodyReader - org.jboss.resteasy.client.ClientResponseFailure: Unable to find a MessageBodyReader of content-type text/plain and type class java.lang.String 找不到内容类型为text / html的MessageBodyReader并键入接口java.util.List - Unable to find a MessageBodyReader of content-type text/html and type interface java.util.List javax.ws.rs.ProcessingException:无法找到内容类型application / json的MessageBodyReader并输入类 - javax.ws.rs.ProcessingException: Unable to find a MessageBodyReader of content-type application/json and type class RESTEASY003145:找不到内容类型 application/json 和类型类 org.keycloak.representations.AccessTokenResponse 的 MessageBodyReader - RESTEASY003145: Unable to find a MessageBodyReader of content-type application/json and type class org.keycloak.representations.AccessTokenResponse HazelCast Rest-Client响应内容-类型配置问题 - HazelCast Rest-Client Response Content -Type Configuration Problem 测试球衣2客户端和webtarget - test jersey 2 client and webtarget Jersey客户端将内容类型标题设置为text / plain - Jersey Client set content-type header as text/plain 使用Jersey客户端下载文件时内容为空 - Empty content-type while downloading file with Jersey client REST客户端的Content-Type标头引发UnsupportedOperationException - REST client Content-Type header throws UnsupportedOperationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM