You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "KARR, DAVID (ATTSI)" <dk...@att.com> on 2011/07/12 17:28:41 UTC

Unit test returning json fails to parse response

I'm building a service using CXF 2.4.1 by reusing most of the setup of a project that was using CXF 2.3.2.  The first project was using Jackson, but I'm going to try using Jettison for this.

My first unit test method uses the default of xml, and that parses the response just fine.  The second method uses the ".json" extension, and I see in the console output that it returned a Jettison-formatted JSON response (it had the namespace prefix on the element name), but it just said this (class name changed):

Jul 12, 2011 8:12:15 AM org.apache.cxf.jaxrs.client.AbstractClient reportMessageHandlerProblem
SEVERE: .Problem with reading the response message, class : class mypackage.Myclass, ContentType : application/json.

I'm referencing "cxf-rt-transports-http" and "cxf-rt-frontend-jaxrs" in my POM.

The following is in my unit test class (with some class names and paths changed):
------------------------
    @BeforeClass
    public static void startServer() {
        setController(new StuffController());
        getController().init();

        ServerFactory.create(getController(), TestHelper.BASE_SERVICE_URI);
    }
    
    @Test
    public void testBasic() throws Exception {
        getController().setService(service);
        
        WebClient   client  = WebClient.create(TestHelper.BASE_SERVICE_URI).path("/stuff");
        
        Stuffs    mockStuffs    = new Stuffs();
        when(service.getStuff()).thenReturn(mockStuffs);
        
        Stuffs    stuffs    = client.get(Stuffs.class);
        assertThat(stuffs).isNotNull();
    }
    
    @Test
    public void testBasicJson() throws Exception {
        getController().setService(service);
        
        WebClient   client  = WebClient.create(TestHelper.BASE_SERVICE_URI).path("/stuff.json");
        
        Stuffs    mockStuffs    = new Stuffs();
        when(service.getStuff()).thenReturn(mockStuffs);
        
        Stuffs    stuffs    = client.get(Stuffs.class);
        assertThat(stuffs).isNotNull();
    }
---------------

This is my ServerFactory class:
----------------------
public class ServerFactory {
    public static void create(Object serviceBean, String uri) {
        JAXRSServerFactoryBean  sf  = new JAXRSServerFactoryBean();

        BindingFactoryManager   manager = sf.getBus().getExtension(BindingFactoryManager.class);
        JAXRSBindingFactory     factory = new JAXRSBindingFactory();
        factory.setBus(sf.getBus());
        manager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, factory);
        
        Map<Object, Object> extensionsMap   = new HashMap<Object, Object>();
        extensionsMap.put("json", "application/json");
        extensionsMap.put("xml", "application/xml");

        sf.setExtensionMappings(extensionsMap);

        sf.setServiceBean(serviceBean);
        sf.getInInterceptors().add(new LoggingInInterceptor());
        sf.getOutInterceptors().add(new LoggingOutInterceptor());
        
        sf.setAddress(uri);
        
        sf.create();
    }
}
--------------------

Re: Unit test returning json fails to parse response

Posted by Sergey Beryozkin <sb...@gmail.com>.
>>
>> I don't recall seeing such NPEs in Jettison - I'll save this stack
>> trace till I get a chance to fix that at jettison level.
>> If you get JSON content with prefixes coming in then you need to
>> explicitly register JSONProvider during WebClient.create(...), and set
>> a namespaceMap property on it (key - prefix, value - namespace).
>> Other approach in 2.4.1 is to configure the *server* provider to drop
>> namespaces completely (ignoreNamespaces property) and use
>> StaxTransformFeature wildcards on the client side to add expected
>> namespaces
>
> I managed to figure this out before I saw this response. :)
>
> I created the JSONProvider in the server factory and added it to both the server and the client, and it now works.
Great...

>
> Actually, Sergey, the namespaceMap is defined in the non-obvious way, with the namespace as the key, and the prefix as the value.
>

Jettison expects it, I guess it's easier this way for Jettison to
react to JAXB reporting writeElement with non-empty namespace...I
guess it could've allocated  prefixes dynamically, which I believe
JSONProvider itself can do (Benson did the work)

Cheers, Sergey

>> com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStar
>> tElement(StAXStreamConnector.java:232)
>> >        at
>> com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StA
>> XStreamConnector.java:176)
>> >        at
>> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Un
>> marshallerImpl.java:360)
>> >        at
>> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unm
>> arshallerImpl.java:332)
>> >        at
>> org.apache.cxf.jaxrs.provider.JSONProvider.readFrom(JSONProvider.java:1
>> 97)
>> >        ... 33 more
>> >
>> >>
>> >> Cheers, Sergey
>> >>
>> >> > I'm referencing "cxf-rt-transports-http" and "cxf-rt-frontend-
>> jaxrs"
>> >> in my POM.
>> >> >
>> >> > The following is in my unit test class (with some class names and
>> >> paths changed):
>> >> > ------------------------
>> >> >    @BeforeClass
>> >> >    public static void startServer() {
>> >> >        setController(new StuffController());
>> >> >        getController().init();
>> >> >
>> >> >        ServerFactory.create(getController(),
>> >> TestHelper.BASE_SERVICE_URI);
>> >> >    }
>> >> >
>> >> >    @Test
>> >> >    public void testBasic() throws Exception {
>> >> >        getController().setService(service);
>> >> >
>> >> >        WebClient   client  =
>> >> WebClient.create(TestHelper.BASE_SERVICE_URI).path("/stuff");
>> >> >
>> >> >        Stuffs    mockStuffs    = new Stuffs();
>> >> >        when(service.getStuff()).thenReturn(mockStuffs);
>> >> >
>> >> >        Stuffs    stuffs    = client.get(Stuffs.class);
>> >> >        assertThat(stuffs).isNotNull();
>> >> >    }
>> >> >
>> >> >    @Test
>> >> >    public void testBasicJson() throws Exception {
>> >> >        getController().setService(service);
>> >> >
>> >> >        WebClient   client  =
>> >> WebClient.create(TestHelper.BASE_SERVICE_URI).path("/stuff.json");
>> >> >
>> >> >        Stuffs    mockStuffs    = new Stuffs();
>> >> >        when(service.getStuff()).thenReturn(mockStuffs);
>> >> >
>> >> >        Stuffs    stuffs    = client.get(Stuffs.class);
>> >> >        assertThat(stuffs).isNotNull();
>> >> >    }
>> >> > ---------------
>> >> >
>> >> > This is my ServerFactory class:
>> >> > ----------------------
>> >> > public class ServerFactory {
>> >> >    public static void create(Object serviceBean, String uri) {
>> >> >        JAXRSServerFactoryBean  sf  = new JAXRSServerFactoryBean();
>> >> >
>> >> >        BindingFactoryManager   manager =
>> >> sf.getBus().getExtension(BindingFactoryManager.class);
>> >> >        JAXRSBindingFactory     factory = new
>> JAXRSBindingFactory();
>> >> >        factory.setBus(sf.getBus());
>> >> >
>> >>
>>  manager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID,
>> >> factory);
>> >> >
>> >> >        Map<Object, Object> extensionsMap   = new HashMap<Object,
>> >> Object>();
>> >> >        extensionsMap.put("json", "application/json");
>> >> >        extensionsMap.put("xml", "application/xml");
>> >> >
>> >> >        sf.setExtensionMappings(extensionsMap);
>> >> >
>> >> >        sf.setServiceBean(serviceBean);
>> >> >        sf.getInInterceptors().add(new LoggingInInterceptor());
>> >> >        sf.getOutInterceptors().add(new LoggingOutInterceptor());
>> >> >
>> >> >        sf.setAddress(uri);
>> >> >
>> >> >        sf.create();
>> >> >    }
>> >> > }
>> >> > --------------------
>> >> >
>> >>
>> >>
>> >>
>> >> --
>> >> Sergey Beryozkin
>> >>
>> >> http://sberyozkin.blogspot.com
>> >> Talend - http://www.talend.com
>> >
>

RE: Unit test returning json fails to parse response

Posted by "KARR, DAVID (ATTSI)" <dk...@att.com>.
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Tuesday, July 12, 2011 9:36 AM
> To: users@cxf.apache.org
> Subject: Re: Unit test returning json fails to parse response
> 
> Hi,
> 
> On Tue, Jul 12, 2011 at 5:11 PM, KARR, DAVID (ATTSI) <dk...@att.com>
> wrote:
> >> -----Original Message-----
> >> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> >> Sent: Tuesday, July 12, 2011 8:57 AM
> >> To: users@cxf.apache.org
> >> Subject: Re: Unit test returning json fails to parse response
> >>
> >> Hi David
> >>
> >> On Tue, Jul 12, 2011 at 4:28 PM, KARR, DAVID (ATTSI)
> <dk...@att.com>
> >> wrote:
> >> > I'm building a service using CXF 2.4.1 by reusing most of the
> setup
> >> of a project that was using CXF 2.3.2.  The first project was using
> >> Jackson, but I'm going to try using Jettison for this.
> >> >
> >> > My first unit test method uses the default of xml, and that parses
> >> the response just fine.  The second method uses the ".json"
> extension,
> >> and I see in the console output that it returned a Jettison-
> formatted
> >> JSON response (it had the namespace prefix on the element name), but
> it
> >> just said this (class name changed):
> >> >
> >> > Jul 12, 2011 8:12:15 AM org.apache.cxf.jaxrs.client.AbstractClient
> >> reportMessageHandlerProblem
> >> > SEVERE: .Problem with reading the response message, class : class
> >> mypackage.Myclass, ContentType : application/json.
> >> >
> >>
> >> In this case you should get ClientWebApplicationException thrown
> >> containing the cause, can you update the code and catch it ? The
> >> exception is coming from the reader, I'm wondering if it's default
> >> JSON provider or not. JAXB provider is ordered first in 2.4.1, so
> >> without WebClient explicitly specifying accept type, it may be
> >> interfering given that MyClass is presumably a JAXB bean
> >
> > The stack trace follows this.  It looks like I need to set up a
> namespace mapping in my factory, and in my client, and probably in my
> Spring context also.
> >
> > org.apache.cxf.jaxrs.client.ClientWebApplicationException: .Problem
> with reading the response message, class : class
> com.att.clarity.constellation.domain.Projects, ContentType :
> application/json.
> <snip/>
> > Caused by: javax.ws.rs.WebApplicationException:
> java.lang.NullPointerException
> >        at
> org.apache.cxf.jaxrs.provider.JSONProvider.readFrom(JSONProvider.java:2
> 17)
> >        at
> org.apache.cxf.jaxrs.client.AbstractClient.readBody(AbstractClient.java
> :435)
> >        ... 32 more
> > Caused by: java.lang.NullPointerException
> >        at org.codehaus.jettison.Node.getNamespaceURI(Node.java:94)
> >        at
> org.codehaus.jettison.AbstractXMLStreamReader.getNamespaceURI(AbstractX
> MLStreamReader.java:133)
> >        at
> org.apache.cxf.staxutils.DepthXMLStreamReader.getNamespaceURI(DepthXMLS
> treamReader.java:135)
> >        at
> 
> I don't recall seeing such NPEs in Jettison - I'll save this stack
> trace till I get a chance to fix that at jettison level.
> If you get JSON content with prefixes coming in then you need to
> explicitly register JSONProvider during WebClient.create(...), and set
> a namespaceMap property on it (key - prefix, value - namespace).
> Other approach in 2.4.1 is to configure the *server* provider to drop
> namespaces completely (ignoreNamespaces property) and use
> StaxTransformFeature wildcards on the client side to add expected
> namespaces

I managed to figure this out before I saw this response. :)

I created the JSONProvider in the server factory and added it to both the server and the client, and it now works.

Actually, Sergey, the namespaceMap is defined in the non-obvious way, with the namespace as the key, and the prefix as the value.

> com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStar
> tElement(StAXStreamConnector.java:232)
> >        at
> com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StA
> XStreamConnector.java:176)
> >        at
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Un
> marshallerImpl.java:360)
> >        at
> com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unm
> arshallerImpl.java:332)
> >        at
> org.apache.cxf.jaxrs.provider.JSONProvider.readFrom(JSONProvider.java:1
> 97)
> >        ... 33 more
> >
> >>
> >> Cheers, Sergey
> >>
> >> > I'm referencing "cxf-rt-transports-http" and "cxf-rt-frontend-
> jaxrs"
> >> in my POM.
> >> >
> >> > The following is in my unit test class (with some class names and
> >> paths changed):
> >> > ------------------------
> >> >    @BeforeClass
> >> >    public static void startServer() {
> >> >        setController(new StuffController());
> >> >        getController().init();
> >> >
> >> >        ServerFactory.create(getController(),
> >> TestHelper.BASE_SERVICE_URI);
> >> >    }
> >> >
> >> >    @Test
> >> >    public void testBasic() throws Exception {
> >> >        getController().setService(service);
> >> >
> >> >        WebClient   client  =
> >> WebClient.create(TestHelper.BASE_SERVICE_URI).path("/stuff");
> >> >
> >> >        Stuffs    mockStuffs    = new Stuffs();
> >> >        when(service.getStuff()).thenReturn(mockStuffs);
> >> >
> >> >        Stuffs    stuffs    = client.get(Stuffs.class);
> >> >        assertThat(stuffs).isNotNull();
> >> >    }
> >> >
> >> >    @Test
> >> >    public void testBasicJson() throws Exception {
> >> >        getController().setService(service);
> >> >
> >> >        WebClient   client  =
> >> WebClient.create(TestHelper.BASE_SERVICE_URI).path("/stuff.json");
> >> >
> >> >        Stuffs    mockStuffs    = new Stuffs();
> >> >        when(service.getStuff()).thenReturn(mockStuffs);
> >> >
> >> >        Stuffs    stuffs    = client.get(Stuffs.class);
> >> >        assertThat(stuffs).isNotNull();
> >> >    }
> >> > ---------------
> >> >
> >> > This is my ServerFactory class:
> >> > ----------------------
> >> > public class ServerFactory {
> >> >    public static void create(Object serviceBean, String uri) {
> >> >        JAXRSServerFactoryBean  sf  = new JAXRSServerFactoryBean();
> >> >
> >> >        BindingFactoryManager   manager =
> >> sf.getBus().getExtension(BindingFactoryManager.class);
> >> >        JAXRSBindingFactory     factory = new
> JAXRSBindingFactory();
> >> >        factory.setBus(sf.getBus());
> >> >
> >>
>  manager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID,
> >> factory);
> >> >
> >> >        Map<Object, Object> extensionsMap   = new HashMap<Object,
> >> Object>();
> >> >        extensionsMap.put("json", "application/json");
> >> >        extensionsMap.put("xml", "application/xml");
> >> >
> >> >        sf.setExtensionMappings(extensionsMap);
> >> >
> >> >        sf.setServiceBean(serviceBean);
> >> >        sf.getInInterceptors().add(new LoggingInInterceptor());
> >> >        sf.getOutInterceptors().add(new LoggingOutInterceptor());
> >> >
> >> >        sf.setAddress(uri);
> >> >
> >> >        sf.create();
> >> >    }
> >> > }
> >> > --------------------
> >> >
> >>
> >>
> >>
> >> --
> >> Sergey Beryozkin
> >>
> >> http://sberyozkin.blogspot.com
> >> Talend - http://www.talend.com
> >

Re: Unit test returning json fails to parse response

Posted by Sergey Beryozkin <sb...@gmail.com>.
>
> I don't recall seeing such NPEs in Jettison - I'll save this stack
> trace till I get a chance to fix that at jettison level.
> If you get JSON content with prefixes coming in then you need to
> explicitly register JSONProvider during WebClient.create(...), and set
> a namespaceMap property on it (key - prefix, value - namespace).
Actually, Christian H reminded me that value is namespace, key is the prefix :-)


> Cheers, Sergey
>

Re: Unit test returning json fails to parse response

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi,

On Tue, Jul 12, 2011 at 5:11 PM, KARR, DAVID (ATTSI) <dk...@att.com> wrote:
>> -----Original Message-----
>> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
>> Sent: Tuesday, July 12, 2011 8:57 AM
>> To: users@cxf.apache.org
>> Subject: Re: Unit test returning json fails to parse response
>>
>> Hi David
>>
>> On Tue, Jul 12, 2011 at 4:28 PM, KARR, DAVID (ATTSI) <dk...@att.com>
>> wrote:
>> > I'm building a service using CXF 2.4.1 by reusing most of the setup
>> of a project that was using CXF 2.3.2.  The first project was using
>> Jackson, but I'm going to try using Jettison for this.
>> >
>> > My first unit test method uses the default of xml, and that parses
>> the response just fine.  The second method uses the ".json" extension,
>> and I see in the console output that it returned a Jettison-formatted
>> JSON response (it had the namespace prefix on the element name), but it
>> just said this (class name changed):
>> >
>> > Jul 12, 2011 8:12:15 AM org.apache.cxf.jaxrs.client.AbstractClient
>> reportMessageHandlerProblem
>> > SEVERE: .Problem with reading the response message, class : class
>> mypackage.Myclass, ContentType : application/json.
>> >
>>
>> In this case you should get ClientWebApplicationException thrown
>> containing the cause, can you update the code and catch it ? The
>> exception is coming from the reader, I'm wondering if it's default
>> JSON provider or not. JAXB provider is ordered first in 2.4.1, so
>> without WebClient explicitly specifying accept type, it may be
>> interfering given that MyClass is presumably a JAXB bean
>
> The stack trace follows this.  It looks like I need to set up a namespace mapping in my factory, and in my client, and probably in my Spring context also.
>
> org.apache.cxf.jaxrs.client.ClientWebApplicationException: .Problem with reading the response message, class : class com.att.clarity.constellation.domain.Projects, ContentType : application/json.
<snip/>
> Caused by: javax.ws.rs.WebApplicationException: java.lang.NullPointerException
>        at org.apache.cxf.jaxrs.provider.JSONProvider.readFrom(JSONProvider.java:217)
>        at org.apache.cxf.jaxrs.client.AbstractClient.readBody(AbstractClient.java:435)
>        ... 32 more
> Caused by: java.lang.NullPointerException
>        at org.codehaus.jettison.Node.getNamespaceURI(Node.java:94)
>        at org.codehaus.jettison.AbstractXMLStreamReader.getNamespaceURI(AbstractXMLStreamReader.java:133)
>        at org.apache.cxf.staxutils.DepthXMLStreamReader.getNamespaceURI(DepthXMLStreamReader.java:135)
>        at

I don't recall seeing such NPEs in Jettison - I'll save this stack
trace till I get a chance to fix that at jettison level.
If you get JSON content with prefixes coming in then you need to
explicitly register JSONProvider during WebClient.create(...), and set
a namespaceMap property on it (key - prefix, value - namespace).
Other approach in 2.4.1 is to configure the *server* provider to drop
namespaces completely (ignoreNamespaces property) and use
StaxTransformFeature wildcards on the client side to add expected
namespaces

Cheers, Sergey


 com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:232)
>        at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:176)
>        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:360)
>        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:332)
>        at org.apache.cxf.jaxrs.provider.JSONProvider.readFrom(JSONProvider.java:197)
>        ... 33 more
>
>>
>> Cheers, Sergey
>>
>> > I'm referencing "cxf-rt-transports-http" and "cxf-rt-frontend-jaxrs"
>> in my POM.
>> >
>> > The following is in my unit test class (with some class names and
>> paths changed):
>> > ------------------------
>> >    @BeforeClass
>> >    public static void startServer() {
>> >        setController(new StuffController());
>> >        getController().init();
>> >
>> >        ServerFactory.create(getController(),
>> TestHelper.BASE_SERVICE_URI);
>> >    }
>> >
>> >    @Test
>> >    public void testBasic() throws Exception {
>> >        getController().setService(service);
>> >
>> >        WebClient   client  =
>> WebClient.create(TestHelper.BASE_SERVICE_URI).path("/stuff");
>> >
>> >        Stuffs    mockStuffs    = new Stuffs();
>> >        when(service.getStuff()).thenReturn(mockStuffs);
>> >
>> >        Stuffs    stuffs    = client.get(Stuffs.class);
>> >        assertThat(stuffs).isNotNull();
>> >    }
>> >
>> >    @Test
>> >    public void testBasicJson() throws Exception {
>> >        getController().setService(service);
>> >
>> >        WebClient   client  =
>> WebClient.create(TestHelper.BASE_SERVICE_URI).path("/stuff.json");
>> >
>> >        Stuffs    mockStuffs    = new Stuffs();
>> >        when(service.getStuff()).thenReturn(mockStuffs);
>> >
>> >        Stuffs    stuffs    = client.get(Stuffs.class);
>> >        assertThat(stuffs).isNotNull();
>> >    }
>> > ---------------
>> >
>> > This is my ServerFactory class:
>> > ----------------------
>> > public class ServerFactory {
>> >    public static void create(Object serviceBean, String uri) {
>> >        JAXRSServerFactoryBean  sf  = new JAXRSServerFactoryBean();
>> >
>> >        BindingFactoryManager   manager =
>> sf.getBus().getExtension(BindingFactoryManager.class);
>> >        JAXRSBindingFactory     factory = new JAXRSBindingFactory();
>> >        factory.setBus(sf.getBus());
>> >
>>  manager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID,
>> factory);
>> >
>> >        Map<Object, Object> extensionsMap   = new HashMap<Object,
>> Object>();
>> >        extensionsMap.put("json", "application/json");
>> >        extensionsMap.put("xml", "application/xml");
>> >
>> >        sf.setExtensionMappings(extensionsMap);
>> >
>> >        sf.setServiceBean(serviceBean);
>> >        sf.getInInterceptors().add(new LoggingInInterceptor());
>> >        sf.getOutInterceptors().add(new LoggingOutInterceptor());
>> >
>> >        sf.setAddress(uri);
>> >
>> >        sf.create();
>> >    }
>> > }
>> > --------------------
>> >
>>
>>
>>
>> --
>> Sergey Beryozkin
>>
>> http://sberyozkin.blogspot.com
>> Talend - http://www.talend.com
>

RE: Unit test returning json fails to parse response

Posted by "KARR, DAVID (ATTSI)" <dk...@att.com>.
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Tuesday, July 12, 2011 8:57 AM
> To: users@cxf.apache.org
> Subject: Re: Unit test returning json fails to parse response
> 
> Hi David
> 
> On Tue, Jul 12, 2011 at 4:28 PM, KARR, DAVID (ATTSI) <dk...@att.com>
> wrote:
> > I'm building a service using CXF 2.4.1 by reusing most of the setup
> of a project that was using CXF 2.3.2.  The first project was using
> Jackson, but I'm going to try using Jettison for this.
> >
> > My first unit test method uses the default of xml, and that parses
> the response just fine.  The second method uses the ".json" extension,
> and I see in the console output that it returned a Jettison-formatted
> JSON response (it had the namespace prefix on the element name), but it
> just said this (class name changed):
> >
> > Jul 12, 2011 8:12:15 AM org.apache.cxf.jaxrs.client.AbstractClient
> reportMessageHandlerProblem
> > SEVERE: .Problem with reading the response message, class : class
> mypackage.Myclass, ContentType : application/json.
> >
> 
> In this case you should get ClientWebApplicationException thrown
> containing the cause, can you update the code and catch it ? The
> exception is coming from the reader, I'm wondering if it's default
> JSON provider or not. JAXB provider is ordered first in 2.4.1, so
> without WebClient explicitly specifying accept type, it may be
> interfering given that MyClass is presumably a JAXB bean

The stack trace follows this.  It looks like I need to set up a namespace mapping in my factory, and in my client, and probably in my Spring context also.

org.apache.cxf.jaxrs.client.ClientWebApplicationException: .Problem with reading the response message, class : class com.att.clarity.constellation.domain.Projects, ContentType : application/json.
	at org.apache.cxf.jaxrs.client.AbstractClient.reportMessageHandlerProblem(AbstractClient.java:589)
	at org.apache.cxf.jaxrs.client.AbstractClient.readBody(AbstractClient.java:438)
	at org.apache.cxf.jaxrs.client.WebClient.handleResponse(WebClient.java:710)
	at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:694)
	at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:634)
	at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:325)
	at org.apache.cxf.jaxrs.client.WebClient.get(WebClient.java:392)
	at com.att.constellation.content.ClarityProjectsControllerTest.testBasicJson(ClarityProjectsControllerTest.java:66)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
	at org.mockito.runners.VerboseMockitoJUnitRunner.run(VerboseMockitoJUnitRunner.java:72)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: javax.ws.rs.WebApplicationException: java.lang.NullPointerException
	at org.apache.cxf.jaxrs.provider.JSONProvider.readFrom(JSONProvider.java:217)
	at org.apache.cxf.jaxrs.client.AbstractClient.readBody(AbstractClient.java:435)
	... 32 more
Caused by: java.lang.NullPointerException
	at org.codehaus.jettison.Node.getNamespaceURI(Node.java:94)
	at org.codehaus.jettison.AbstractXMLStreamReader.getNamespaceURI(AbstractXMLStreamReader.java:133)
	at org.apache.cxf.staxutils.DepthXMLStreamReader.getNamespaceURI(DepthXMLStreamReader.java:135)
	at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:232)
	at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:176)
	at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:360)
	at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:332)
	at org.apache.cxf.jaxrs.provider.JSONProvider.readFrom(JSONProvider.java:197)
	... 33 more

> 
> Cheers, Sergey
> 
> > I'm referencing "cxf-rt-transports-http" and "cxf-rt-frontend-jaxrs"
> in my POM.
> >
> > The following is in my unit test class (with some class names and
> paths changed):
> > ------------------------
> >    @BeforeClass
> >    public static void startServer() {
> >        setController(new StuffController());
> >        getController().init();
> >
> >        ServerFactory.create(getController(),
> TestHelper.BASE_SERVICE_URI);
> >    }
> >
> >    @Test
> >    public void testBasic() throws Exception {
> >        getController().setService(service);
> >
> >        WebClient   client  =
> WebClient.create(TestHelper.BASE_SERVICE_URI).path("/stuff");
> >
> >        Stuffs    mockStuffs    = new Stuffs();
> >        when(service.getStuff()).thenReturn(mockStuffs);
> >
> >        Stuffs    stuffs    = client.get(Stuffs.class);
> >        assertThat(stuffs).isNotNull();
> >    }
> >
> >    @Test
> >    public void testBasicJson() throws Exception {
> >        getController().setService(service);
> >
> >        WebClient   client  =
> WebClient.create(TestHelper.BASE_SERVICE_URI).path("/stuff.json");
> >
> >        Stuffs    mockStuffs    = new Stuffs();
> >        when(service.getStuff()).thenReturn(mockStuffs);
> >
> >        Stuffs    stuffs    = client.get(Stuffs.class);
> >        assertThat(stuffs).isNotNull();
> >    }
> > ---------------
> >
> > This is my ServerFactory class:
> > ----------------------
> > public class ServerFactory {
> >    public static void create(Object serviceBean, String uri) {
> >        JAXRSServerFactoryBean  sf  = new JAXRSServerFactoryBean();
> >
> >        BindingFactoryManager   manager =
> sf.getBus().getExtension(BindingFactoryManager.class);
> >        JAXRSBindingFactory     factory = new JAXRSBindingFactory();
> >        factory.setBus(sf.getBus());
> >
>  manager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID,
> factory);
> >
> >        Map<Object, Object> extensionsMap   = new HashMap<Object,
> Object>();
> >        extensionsMap.put("json", "application/json");
> >        extensionsMap.put("xml", "application/xml");
> >
> >        sf.setExtensionMappings(extensionsMap);
> >
> >        sf.setServiceBean(serviceBean);
> >        sf.getInInterceptors().add(new LoggingInInterceptor());
> >        sf.getOutInterceptors().add(new LoggingOutInterceptor());
> >
> >        sf.setAddress(uri);
> >
> >        sf.create();
> >    }
> > }
> > --------------------
> >
> 
> 
> 
> --
> Sergey Beryozkin
> 
> http://sberyozkin.blogspot.com
> Talend - http://www.talend.com

Re: Unit test returning json fails to parse response

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi David

On Tue, Jul 12, 2011 at 4:28 PM, KARR, DAVID (ATTSI) <dk...@att.com> wrote:
> I'm building a service using CXF 2.4.1 by reusing most of the setup of a project that was using CXF 2.3.2.  The first project was using Jackson, but I'm going to try using Jettison for this.
>
> My first unit test method uses the default of xml, and that parses the response just fine.  The second method uses the ".json" extension, and I see in the console output that it returned a Jettison-formatted JSON response (it had the namespace prefix on the element name), but it just said this (class name changed):
>
> Jul 12, 2011 8:12:15 AM org.apache.cxf.jaxrs.client.AbstractClient reportMessageHandlerProblem
> SEVERE: .Problem with reading the response message, class : class mypackage.Myclass, ContentType : application/json.
>

In this case you should get ClientWebApplicationException thrown
containing the cause, can you update the code and catch it ? The
exception is coming from the reader, I'm wondering if it's default
JSON provider or not. JAXB provider is ordered first in 2.4.1, so
without WebClient explicitly specifying accept type, it may be
interfering given that MyClass is presumably a JAXB bean

Cheers, Sergey

> I'm referencing "cxf-rt-transports-http" and "cxf-rt-frontend-jaxrs" in my POM.
>
> The following is in my unit test class (with some class names and paths changed):
> ------------------------
>    @BeforeClass
>    public static void startServer() {
>        setController(new StuffController());
>        getController().init();
>
>        ServerFactory.create(getController(), TestHelper.BASE_SERVICE_URI);
>    }
>
>    @Test
>    public void testBasic() throws Exception {
>        getController().setService(service);
>
>        WebClient   client  = WebClient.create(TestHelper.BASE_SERVICE_URI).path("/stuff");
>
>        Stuffs    mockStuffs    = new Stuffs();
>        when(service.getStuff()).thenReturn(mockStuffs);
>
>        Stuffs    stuffs    = client.get(Stuffs.class);
>        assertThat(stuffs).isNotNull();
>    }
>
>    @Test
>    public void testBasicJson() throws Exception {
>        getController().setService(service);
>
>        WebClient   client  = WebClient.create(TestHelper.BASE_SERVICE_URI).path("/stuff.json");
>
>        Stuffs    mockStuffs    = new Stuffs();
>        when(service.getStuff()).thenReturn(mockStuffs);
>
>        Stuffs    stuffs    = client.get(Stuffs.class);
>        assertThat(stuffs).isNotNull();
>    }
> ---------------
>
> This is my ServerFactory class:
> ----------------------
> public class ServerFactory {
>    public static void create(Object serviceBean, String uri) {
>        JAXRSServerFactoryBean  sf  = new JAXRSServerFactoryBean();
>
>        BindingFactoryManager   manager = sf.getBus().getExtension(BindingFactoryManager.class);
>        JAXRSBindingFactory     factory = new JAXRSBindingFactory();
>        factory.setBus(sf.getBus());
>        manager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, factory);
>
>        Map<Object, Object> extensionsMap   = new HashMap<Object, Object>();
>        extensionsMap.put("json", "application/json");
>        extensionsMap.put("xml", "application/xml");
>
>        sf.setExtensionMappings(extensionsMap);
>
>        sf.setServiceBean(serviceBean);
>        sf.getInInterceptors().add(new LoggingInInterceptor());
>        sf.getOutInterceptors().add(new LoggingOutInterceptor());
>
>        sf.setAddress(uri);
>
>        sf.create();
>    }
> }
> --------------------
>



-- 
Sergey Beryozkin

http://sberyozkin.blogspot.com
Talend - http://www.talend.com