Hi Sergey, 

I don't quite understand the proper implementation for using CXF and JSON. 
I have the xml implementation working, see below.  Can you guide me on doing
the same basic function but returning JSON?  I know I need to change the
@Produces to "application/json", but what setup do I need to do in order to
return the json string representing the Object "o" to the browser or
consumer of the service?  Thanks for the help!


xml version:

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
public class XMLExample{
        @GET    
        @Path("/getTopSpamSender")
        @Produces("application/xml")
        public Response retrieveTopSpamSenderData() throws Exception {  
                
                Response httpresp;
                String httpbody;        
                httpresp=null;
                
                Object o = topSpamSenderService.getTopSpamSender(null);
                
                if (o != null)
                {
                                                        
                        httpbody = JaxbUtils.ObjToXmlStr(o, false);
                        httpresp = 
setupResponse(Response.ok(httpbody)).build();                                   
     
                }
                else    
                        httpresp = setupResponse(Response.noContent()).build(); 
                
                        
                
                                
                return httpresp;

        }
}

public static String ObjToXmlStr(Object obj, boolean includeXmlVerHdr)
        {
                try
                {
                        StringWriter sw = new StringWriter();
                        JAXBContext jaxbctx = 
JAXBContext.newInstance(obj.getClass());
                        Marshaller marshaller = jaxbctx.createMarshaller();
                        marshaller.setProperty(Marshaller.JAXB_ENCODING, 
"UTF-8");
                        
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                        
                        marshaller.marshal(obj, sw);
                        
                        if (!includeXmlVerHdr)
                        {
                                String str = sw.toString();
                                int beginIndex = str.toString().indexOf(">");
                                
                                return str.substring(beginIndex + 2);
                        }
                        
                        return sw.toString();
                        
                } catch (Exception e)
                {
                        log.error("Failed to create XML from Object", e);
                }
                
                return null;
        }


Sergey Beryozkin-2 wrote:
> 
> Thanks David...
> 
> Looks like the problem has to do with that fact that an Object instance is
> being returned....
> The method.getReturnType() is used when searching for providers...
> 
> cheers, Sergey
> 
> 
> 
>> -----Original Message-----
>> From: cgswtsu78 [mailto:[email protected]]
>> Sent: Tuesday, December 01, 2009 3:59 PM
>> To: [email protected]
>> Subject: RE: How to use CXF and JSON
>> 
>> 
>> When I do the below I get the below exception.  EnvFromHourlyWrapper
> is
>> my
>> wrapper object that holds a List of the objects that I want to return.
> 
> I believe if you Google for "JAXRSOutInterceptor
> writeResponseErrorMessage WARNING: No message body writer has been found
> for response class", you'll find an explanation of this.
> 
>> cgswtsu78 wrote:
>> >
>> > So the below will return the json representation of the Object o?
> No
>> > other setup is needed?  Thanks for the quick responses!
>> >
>> >
>> >         @GET
>> > @Path("/topspamsenderjson")
>> > @Produces("application/json")
>> > public Object getTopSpamSenderData() throws Exception
>> > {
>> > Object o = topSpamSenderService.getTopSpamSender();
>> >
>> > return o;
>> > }
>> >
>> >
>> >
>> > KARR, DAVID (ATTCINW) wrote:
>> >>
>> >>> -----Original Message-----
>> >>> From: cgswtsu78 [mailto:[email protected]]
>> >>> Sent: Tuesday, December 01, 2009 3:07 PM
>> >>> To: [email protected]
>> >>> Subject: RE: How to use CXF and JSON
>> >>>
>> >>>
>> >>> Ok, I've made that switch, but I'm still confused as how I need to
>> >>> setup the
>> >>> javax.ws.rs.core.Response.  How would I take an Object and convert
>> it
>> >>> into a
>> >>> json string?  Below is the xml sample I created, any suggestions
> on
>> >>> converting this to a json implementation would be greatly
>> appreciated.
>> >>
>> >> I wondered what you were talking about in your previous note.
>> >> Typically, you don't do anything to format the response except for
>> >> returning the object from the method.  The container does the work
>> of
>> >> formatting the response in either XML or JSON.  Most of this code
>> you've
>> >> written wasn't necessary.  You can modify the formatting of the
>> response
>> >> with "@Xml..." annotations (which also can affect the JSON output),
>> but
>> >> mostly you just let the container do the work.
>> >>
>> >>> KARR, DAVID (ATTCINW) wrote:
>> >>> >
>> >>> >> -----Original Message-----
>> >>> >> From: cgswtsu78 [mailto:[email protected]]
>> >>> >> Sent: Tuesday, December 01, 2009 2:44 PM
>> >>> >> To: [email protected]
>> >>> >> Subject: How to use CXF and JSON
>> >>> >>
>> >>> >>
>> >>> >> Hello,
>> >>> >>
>> >>> >> I'm very new to building RESTful webservices using apache cxf
>> and I
>> >>> >> currently have a small sample that returns a
>> >>> javax.ws.rs.core.Response
>> >>> >> in
>> >>> >> xml format using the @ProduceMime("application/xml").  My
>> question
>> >>> is
>> >>> >> how do
>> >>> >> I return a javax.ws.rs.core.Response in JSON format?  I've
> tried
>> >>> using
>> >>> >> @ProduceMime("text/json"), @ProduceMime("application/json").
>> I'm
>> >>> > using
>> >>> >> JAXB
>> >>> >> to convert the object to xml and then rebuilding the response,
>> but
>> >>> I'm
>> >>> >> not
>> >>> >> sure how to rebuild the response when I return json.  Is this
>> >>> possible
>> >>> >> with
>> >>> >> CXF?
>> >>> >
>> >>> > The "old" "@ProduceMime" annotation has been replaced with
>> >>> "@Produces".
>> >>> >
>> >>> > The following will make a method produce either XML or JSON,
>> >>> depending
>> >>> > on the Accept header value:
>> >>> >
>> >>> > @Produces({"application/xml", "application/json"})
>> >>> >
>> >>> >
>> >>>
>> >>> --
>> >>> View this message in context:
>> >> http://old.nabble.com/How-to-use-CXF-and-
>> >>> JSON-tp26600386p26600710.html
>> >>> Sent from the cxf-user mailing list archive at Nabble.com.
>> >>
>> >>
>> >>
>> >
>> >
>> 
>> --
>> View this message in context:
> http://old.nabble.com/How-to-use-CXF-and-
>> JSON-tp26600386p26601041.html
>> Sent from the cxf-user mailing list archive at Nabble.com.
> 
> 
> 

-- 
View this message in context: 
http://old.nabble.com/How-to-use-CXF-and-JSON-tp26600386p26612855.html
Sent from the cxf-user mailing list archive at Nabble.com.

Reply via email to