尝试使用JAX-RS POST XML时的空指针

时间:2016-01-25 18:48:35

标签: java jax-rs jersey-2.0 jersey-client

尝试将XML文档(作为DOMSource)发布到服务时,我收到空指针异常。我无法访问服务器端代码,而且只与客户端一起工作。

Document docRequest = ...;
...
Entity<DOMSource> docEntity = Entity.xml(new DOMSource(docRequest));
Invocation.Builder requestBuilder = client.target(endpoint).request(MediaType.APPLICATION_XML_TYPE).accept(MediaType.APPLICATION_XML_TYPE);
Invocation invoker = requestBuilder.buildPost(docEntity);
String rawResponse = invoker.invoke(String.class);

导致此例外:

Caused by: java.lang.NullPointerException
at org.glassfish.jersey.message.internal.SourceProvider$SourceWriter.writeTo(SourceProvider.java:235)
at org.glassfish.jersey.message.internal.SourceProvider$SourceWriter.writeTo(SourceProvider.java:196)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:265)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:250)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130)
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:502)
at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:388)
at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:285)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:255)

我到目前为止所确定的是,生成Source对象的提供程序(org.glassfish.jersey.message.internal.SourceProvider $ SourceWriter)正在将其saxParserFactory和transformerFactory实例变量设置为null。这些值永远不会再次设置。

来自泽西岛的消息来源:

    private final Factory<SAXParserFactory> saxParserFactory;
    private final Factory<TransformerFactory> transformerFactory;

    public SourceWriter(@Context Factory<SAXParserFactory> spf,
            @Context Factory<TransformerFactory> tf) {
        this.saxParserFactory = spf;
        this.transformerFactory = tf;
    }

导致NullPointerException的代码:

public void writeTo(Source source, Class<?> t, Type gt, Annotation[] as, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
...
transformerFactory.provide().newTransformer().transform(source, sr);

有人可以提供一些关于为什么这些字段没有设置为非空值的上下文吗?

0 个答案:

没有答案