Return-Path: Delivered-To: apmail-cxf-users-archive@www.apache.org Received: (qmail 74360 invoked from network); 2 Dec 2009 18:54:20 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 2 Dec 2009 18:54:20 -0000 Received: (qmail 78123 invoked by uid 500); 2 Dec 2009 18:54:20 -0000 Delivered-To: apmail-cxf-users-archive@cxf.apache.org Received: (qmail 78034 invoked by uid 500); 2 Dec 2009 18:54:19 -0000 Mailing-List: contact users-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@cxf.apache.org Delivered-To: mailing list users@cxf.apache.org Received: (qmail 78024 invoked by uid 99); 2 Dec 2009 18:54:19 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Dec 2009 18:54:19 +0000 X-ASF-Spam-Status: No, hits=-6.0 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_MED X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: local policy) Received: from [216.82.250.83] (HELO mail120.messagelabs.com) (216.82.250.83) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Dec 2009 18:54:16 +0000 X-VirusChecked: Checked X-Env-Sender: dk068x@att.com X-Msg-Ref: server-12.tower-120.messagelabs.com!1259780034!51085362!1 X-StarScan-Version: 6.2.4; banners=-,-,- X-Originating-IP: [144.160.112.25] Received: (qmail 866 invoked from network); 2 Dec 2009 18:53:55 -0000 Received: from sbcsmtp3.sbc.com (HELO tlph064.enaf.dadc.sbc.com) (144.160.112.25) by server-12.tower-120.messagelabs.com with DHE-RSA-AES256-SHA encrypted SMTP; 2 Dec 2009 18:53:55 -0000 Received: from enaf.dadc.sbc.com (localhost.localdomain [127.0.0.1]) by tlph064.enaf.dadc.sbc.com (8.14.3/8.14.3) with ESMTP id nB2IrrLu014061 for ; Wed, 2 Dec 2009 12:53:54 -0600 Received: from td03xsmtp006.US.Cingular.Net (td03xspare19-new.us.cingular.net [135.179.64.43] (may be forged)) by tlph064.enaf.dadc.sbc.com (8.14.3/8.14.3) with ESMTP id nB2IrlnL013888 for ; Wed, 2 Dec 2009 12:53:48 -0600 Received: from bd01xsmtp004.US.Cingular.Net ([135.163.18.45]) by td03xsmtp006.US.Cingular.Net with Microsoft SMTPSVC(6.0.3790.3959); Wed, 2 Dec 2009 12:53:48 -0600 Received: from BD01MSXMB018.US.Cingular.Net ([135.214.27.52]) by bd01xsmtp004.US.Cingular.Net with Microsoft SMTPSVC(6.0.3790.3959); Wed, 2 Dec 2009 10:53:46 -0800 x-mimeole: Produced By Microsoft Exchange V6.5 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: RE: How to use CXF and JSON Date: Wed, 2 Dec 2009 10:53:46 -0800 Message-ID: In-Reply-To: <26613741.post@talk.nabble.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: How to use CXF and JSON Thread-Index: Acpze8Ste1PYfpXJQIuAk+OPVNO7oQABOHXQ References: <26600386.post@talk.nabble.com> <26600710.post@talk.nabble.com> <26601037.post@talk.nabble.com> <26601041.post@talk.nabble.com> <027201ca7362$f44b1130$1f02050a@emea.progress.com> <26612855.post@talk.nabble.com> <036801ca737a$beb333a0$1f02050a@emea.progress.com> <26613741.post@talk.nabble.com> From: "KARR, DAVID (ATTCINW)" To: X-OriginalArrivalTime: 02 Dec 2009 18:53:46.0871 (UTC) FILETIME=[C7364070:01CA7380] > -----Original Message----- > From: cgswtsu78 [mailto:cgray@proofpoint.com] > Sent: Wednesday, December 02, 2009 10:17 AM > To: users@cxf.apache.org > Subject: Re: How to use CXF and JSON >=20 >=20 > That did work, but it required me to save the json string as a file. > When > opening it in notepad it has the correct object representation. How > can I > display the json string in the browser? Do I have to setup something > prior > to making the request to the service method? Currently, I'm just > executing > a GET request by pasting in a url in IE. Then don't do that. :) Use SoapUI, or some other tool that knows JAX-RS. It can even read the WADL specification and generate the tests for you. > Sergey Beryozkin-2 wrote: > > > > Hi > > > > You probably just don't need to do it all, just do > > > > @Produces(application/xml, application/json) > > @GET > > public SomeConcreteObjectType get(...) { > > return topSpamSenderService.getTopSpamSender(null); > > } > > > > hope it helps > > > > Sergey > > > >> > >> 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=3Dnull; > >> > >> Object o =3D topSpamSenderService.getTopSpamSender(null); > >> > >> if (o !=3D null) > >> { > >> > >> httpbody =3D JaxbUtils.ObjToXmlStr(o, false); > >> httpresp =3D setupResponse(Response.ok(httpbody)).build(); > >> } > >> else > >> httpresp =3D setupResponse(Response.noContent()).build(); > >> > >> > >> > >> return httpresp; > >> > >> } > >> } > >> > >> public static String ObjToXmlStr(Object obj, boolean > includeXmlVerHdr) > >> { > >> try > >> { > >> StringWriter sw =3D new StringWriter(); > >> JAXBContext jaxbctx =3D JAXBContext.newInstance(obj.getClass()); > >> Marshaller marshaller =3D jaxbctx.createMarshaller(); > >> marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); > >> marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); > >> > >> marshaller.marshal(obj, sw); > >> > >> if (!includeXmlVerHdr) > >> { > >> String str =3D sw.toString(); > >> int beginIndex =3D 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:cgray@proofpoint.com] > >>>> Sent: Tuesday, December 01, 2009 3:59 PM > >>>> To: users@cxf.apache.org > >>>> 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 =3D topSpamSenderService.getTopSpamSender(); > >>>> > > >>>> > return o; > >>>> > } > >>>> > > >>>> > > >>>> > > >>>> > KARR, DAVID (ATTCINW) wrote: > >>>> >> > >>>> >>> -----Original Message----- > >>>> >>> From: cgswtsu78 [mailto:cgray@proofpoint.com] > >>>> >>> Sent: Tuesday, December 01, 2009 3:07 PM > >>>> >>> To: users@cxf.apache.org > >>>> >>> 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:cgray@proofpoint.com] > >>>> >>> >> Sent: Tuesday, December 01, 2009 2:44 PM > >>>> >>> >> To: users@cxf.apache.org > >>>> >>> >> 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. > >> > > > > >=20 > -- > View this message in context: http://old.nabble.com/How-to-use-CXF-and- > JSON-tp26600386p26613741.html > Sent from the cxf-user mailing list archive at Nabble.com.