[ 
https://issues.apache.org/jira/browse/CXF-6060?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14181685#comment-14181685
 ] 

Nikolai Amelichev commented on CXF-6060:
----------------------------------------

Workaround: before calling {{readEntity()}}, create a fake 
{{org.apache.cxf.message.MessageImpl}} and set it as the {{outMessage}}:

{code:java}
    QName qname = new QName("qname");
    JAXRSServiceImpl service = new JAXRSServiceImpl("localhost", qname);
    EndpointImpl endpoint = null;
    try {
      EndpointInfo ei = new EndpointInfo();
      endpoint = new EndpointImpl(null, service, ei);
      endpoint.put("org.apache.cxf.jaxrs.provider.ServerProviderFactory", new 
ProviderFactory(null, null) {
        @Override
        protected void setProviders(Object... providers) {
        }

        @Override
        public <T> List<ReaderInterceptor> 
createMessageBodyReaderInterceptor(Class<T> bodyType, Type parameterType, 
Annotation[] parameterAnnotations, MediaType mediaType, Message m, boolean 
checkMbrNow, Set<String> names) {
          return null;
        }
      });
    } catch (EndpointException e) {
      e.printStackTrace();
    }

    ExchangeImpl exchange = new ExchangeImpl();
    exchange.put(Endpoint.class, endpoint);

    MessageImpl msg = new MessageImpl();
    msg.setExchange(exchange);

    ((ResponseImpl)response).setOutMessage(msg);
{code}

> Calling readEntity() on Response created by Response.build() causes 
> NullPointerException
> ----------------------------------------------------------------------------------------
>
>                 Key: CXF-6060
>                 URL: https://issues.apache.org/jira/browse/CXF-6060
>             Project: CXF
>          Issue Type: Bug
>          Components: JAX-RS
>    Affects Versions: 3.0.1
>         Environment: Ubuntu 14.04.1 LTS, x86_64.
> java version "1.8.0_25"
> Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
> Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
>            Reporter: Nikolai Amelichev
>
> This small test:
> {code:java}
> @Test
> public void should_read_entity() {
>     final String str = "ouch";
>     Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
>               .entity(str)
>               .build();
>     Assert.assertEquals(str, response.readEntity(String.class));
> }
> {code}
> unexpectedly throws `NullPointerException` with the following stacktrace:
> {noformat}
> at 
> org.apache.cxf.jaxrs.provider.ProviderFactory.getInstance(ProviderFactory.java:950)
> at org.apache.cxf.jaxrs.impl.ResponseImpl.doReadEntity(ResponseImpl.java:357)
> at org.apache.cxf.jaxrs.impl.ResponseImpl.readEntity(ResponseImpl.java:319)
> at
> org.apache.cxf.jaxrs.impl.ResponseImpl.readEntity(ResponseImpl.java:309)
> {noformat}
> (because {{Response}} created by {{Response.build()}} has {{outMessage == 
> null}}).
> JAX-RS specifies that {{Response.readEntity()}} throws only 
> {{ProcessingException}} and {{IllegalStateException}}. None of them apply: 
> response entity can be converted to {{String}}, and response is not closed. 
> So this test should pass (with {{Response.readEntity()}} returning 
> {{"ouch"}}).



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to