RestEasy客户端未使用正确的编码

时间:2018-07-17 10:20:31

标签: resteasy

我使用RestEasy作为客户端从服务中读取新闻。

    ResteasyClient listClient = new ResteasyClientBuilder().build();
    ResteasyWebTarget listTarget = listClient.target("https://someservice.com/file.xml");
    Response r = listTarget.request().get();
    final HexMl feedList = r.readEntity(HexMl.class);

该服务在响应标头中不返回编码或媒体类型,仅在xml本身中返回编码

<?xml version="1.0" encoding="windows-1252"?>

RestEasy似乎没有对此进行评估,所以我得到了一个例外:

javax.ws.rs.ProcessingException: org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException: javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 116; columnNumber: 30; Invalid byte 2 of 3-byte UTF-8 sequence.]
    at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:300)
    at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readEntity(ClientResponse.java:196)
    at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:218)
    at com.roche.services.NewsImportService.importFeed(NewsImportService.java:72)
    at com.roche.commands.NewsImportCommand.execute(NewsImportCommand.java:26)
    at com.roche.commands.NewsImportCommand$execute.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:122)
    at Script1.run(Script1.groovy:4)
    at info.magnolia.module.groovy.console.MgnlGroovyConsole$1.call(MgnlGroovyConsole.java:154)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException: javax.xml.bind.UnmarshalException

有没有一种方法可以覆盖RestEasy使用的编码或在读取实体之前拦截响应? 我尝试过

Response r = listTarget.request().accept(APPLICATION_XML + ";charset=windows-1252").get();

Response r = listTarget.request(APPLICATION_XML + ";charset=windows-1252").get();

@Consumes(APPLICATION_XML + ";charset=windows-1252")
public class HexMl { ... }

没有成功。 XML本身似乎已在Windows-1252中正确编码。

1 个答案:

答案 0 :(得分:0)

现在我正在使用ReaderInterceptor,但这似乎不正确。因此,我仍然对更好的建议感到高兴。

ResteasyClientBuilder clientBuilder = new ResteasyClientBuilder();
ResteasyProviderFactory providerFactory = new ResteasyProviderFactory();
RegisterBuiltin.register( providerFactory );
providerFactory.getClientReaderInterceptorRegistry().registerSingleton( new ReaderInterceptor() {
    @Override
    public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
            InputStream is = context.getInputStream();
            String responseBody = IOUtils.toString( is , "windows-1252");

            LOGGER.debug( "received response:\n{}\n\n", responseBody );

            context.setInputStream( new ByteArrayInputStream( responseBody.getBytes() ) );
        return context.proceed();
    }
} );
clientBuilder.providerFactory( providerFactory );

然后我使用此clientBuilder创建我的客户端。