You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by cgswtsu78 <cg...@proofpoint.com> on 2009/12/01 23:43:57 UTC

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?

Thanks, 

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


RE: How to use CXF and JSON

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi Colin

Just replace get() with say post(objectInstance, CustomObject.class) or
just post(objectInstance). There's also a method called invoke() which
accepts an HTTP method name as the 1st parameter

Cheers, Sergey

-----Original Message-----
From: cgswtsu78 [mailto:cgray@proofpoint.com] 
Sent: 09 December 2009 00:04
To: users@cxf.apache.org
Subject: Re: How to use CXF and JSON


Sergey, 

how would you mimic a POST request with the WebClient?  It works fine
for a
GET request.  I need to post an entire object to the web service method
so I
would prefer using a POST.  Any suggestions or guidelines for this?

Thanks, 

Colin


Sergey Beryozkin-2 wrote:
> 
> Perhaps you can use a CXF client api instead ?
> try
> 
> MyObject object =
>
WebClient.create(address).accept("application/xml,application/json").get
(MyObject.class)
> 
> cheers, Sergey
> 
> 
>> 
>> David or Sergey, 
>> 
>> I guess back to my original question....except this should be a bit
more
>> clear.  
>> 
>> I'm using apache's httpclient in order to process POST and GET
requests
>> to
>> my REST service methods.  These POST and GET requests are being
executed
>> from a JSF managed bean in order to retrieve an object and then
perform
>> operations on that object before displaying it in a xhtml page.
While
>> using
>> xml I'm able to return the HttpResponse from the service method and
use
>> the
>> HttpResponse.getBodyAsString() along with JAXB in order to convert
the
>> xml
>> string to an object.  
>> 
>> For JSON, what would be the best way to return the JSON string and
then
>> convert that to an object so that I can return that to the consumer
of
>> the
>> service method (JSF managed bean) in order for it to perform
operations
>> on
>> it? 
>> 
>> 
>> 
>> 
>> KARR, DAVID (ATTCINW) wrote:
>>> 
>>>> -----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
>>>> 
>>>> 
>>>> 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=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: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 = 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.
>>>> >>
>>>> >
>>>> >
>>>> 
>>>> --
>>>> 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.
>>> 
>>> 
>>> 
>> 
>> -- 
>> View this message in context:
>>
http://old.nabble.com/How-to-use-CXF-and-JSON-tp26600386p26616947.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-tp26600386p26703083.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: How to use CXF and JSON

Posted by cgswtsu78 <cg...@proofpoint.com>.
Sergey, 

how would you mimic a POST request with the WebClient?  It works fine for a
GET request.  I need to post an entire object to the web service method so I
would prefer using a POST.  Any suggestions or guidelines for this?

Thanks, 

Colin


Sergey Beryozkin-2 wrote:
> 
> Perhaps you can use a CXF client api instead ?
> try
> 
> MyObject object =
> WebClient.create(address).accept("application/xml,application/json").get(MyObject.class)
> 
> cheers, Sergey
> 
> 
>> 
>> David or Sergey, 
>> 
>> I guess back to my original question....except this should be a bit more
>> clear.  
>> 
>> I'm using apache's httpclient in order to process POST and GET requests
>> to
>> my REST service methods.  These POST and GET requests are being executed
>> from a JSF managed bean in order to retrieve an object and then perform
>> operations on that object before displaying it in a xhtml page.  While
>> using
>> xml I'm able to return the HttpResponse from the service method and use
>> the
>> HttpResponse.getBodyAsString() along with JAXB in order to convert the
>> xml
>> string to an object.  
>> 
>> For JSON, what would be the best way to return the JSON string and then
>> convert that to an object so that I can return that to the consumer of
>> the
>> service method (JSF managed bean) in order for it to perform operations
>> on
>> it? 
>> 
>> 
>> 
>> 
>> KARR, DAVID (ATTCINW) wrote:
>>> 
>>>> -----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
>>>> 
>>>> 
>>>> 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=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: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 = 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.
>>>> >>
>>>> >
>>>> >
>>>> 
>>>> --
>>>> 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.
>>> 
>>> 
>>> 
>> 
>> -- 
>> View this message in context:
>> http://old.nabble.com/How-to-use-CXF-and-JSON-tp26600386p26616947.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-tp26600386p26703083.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: How to use CXF and JSON

Posted by Sergey Beryozkin <sb...@progress.com>.
Perhaps you can use a CXF client api instead ?
try

MyObject object = WebClient.create(address).accept("application/xml,application/json").get(MyObject.class)

cheers, Sergey


> 
> David or Sergey, 
> 
> I guess back to my original question....except this should be a bit more
> clear.  
> 
> I'm using apache's httpclient in order to process POST and GET requests to
> my REST service methods.  These POST and GET requests are being executed
> from a JSF managed bean in order to retrieve an object and then perform
> operations on that object before displaying it in a xhtml page.  While using
> xml I'm able to return the HttpResponse from the service method and use the
> HttpResponse.getBodyAsString() along with JAXB in order to convert the xml
> string to an object.  
> 
> For JSON, what would be the best way to return the JSON string and then
> convert that to an object so that I can return that to the consumer of the
> service method (JSF managed bean) in order for it to perform operations on
> it? 
> 
> 
> 
> 
> KARR, DAVID (ATTCINW) wrote:
>> 
>>> -----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
>>> 
>>> 
>>> 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=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: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 = 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.
>>> >>
>>> >
>>> >
>>> 
>>> --
>>> 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.
>> 
>> 
>> 
> 
> -- 
> View this message in context: http://old.nabble.com/How-to-use-CXF-and-JSON-tp26600386p26616947.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>

RE: How to use CXF and JSON

Posted by cgswtsu78 <cg...@proofpoint.com>.
David or Sergey, 

I guess back to my original question....except this should be a bit more
clear.  

I'm using apache's httpclient in order to process POST and GET requests to
my REST service methods.  These POST and GET requests are being executed
from a JSF managed bean in order to retrieve an object and then perform
operations on that object before displaying it in a xhtml page.  While using
xml I'm able to return the HttpResponse from the service method and use the
HttpResponse.getBodyAsString() along with JAXB in order to convert the xml
string to an object.  

For JSON, what would be the best way to return the JSON string and then
convert that to an object so that I can return that to the consumer of the
service method (JSF managed bean) in order for it to perform operations on
it? 




KARR, DAVID (ATTCINW) wrote:
> 
>> -----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
>> 
>> 
>> 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=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: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 = 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.
>> >>
>> >
>> >
>> 
>> --
>> 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.
> 
> 
> 

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


RE: How to use CXF and JSON

Posted by cgswtsu78 <cg...@proofpoint.com>.
David or Sergey, 

I guess back to my original question....except this should be a bit more
clear.  

I'm using apache's httpclient in order to process POST and GET requests to
my REST service methods.  These POST and GET requests are being executed
from a JSF managed bean in order to retrieve an object and then perform
operations on that object before displaying it in a xhtml page.  While using
xml I'm able to return the HttpResponse from the service method and use the
HttpResponse.getBodyAsString() along with JAXB in order to convert the xml
string to an object.  

For JSON, what would be the best way to return the JSON string and then
convert that to an object so that I can return that to the consumer of the
service method (JSF managed bean) in order for it to perform operations on
it? 


KARR, DAVID (ATTCINW) wrote:
> 
>> -----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
>> 
>> 
>> 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=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: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 = 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.
>> >>
>> >
>> >
>> 
>> --
>> 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.
> 
> 
> 

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


RE: How to use CXF and JSON

Posted by "KARR, DAVID (ATTCINW)" <dk...@att.com>.
> -----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
> 
> 
> 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=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: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 = 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.
> >>
> >
> >
> 
> --
> 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.


Re: How to use CXF and JSON

Posted by cgswtsu78 <cg...@proofpoint.com>.
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.  



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=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: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 = 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.
>>
> 
> 

-- 
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.


Re: How to use CXF and JSON

Posted by Sergey Beryozkin <sb...@progress.com>.
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=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: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 = 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.
>

Re: How to use CXF and JSON

Posted by cgswtsu78 <cg...@proofpoint.com>.
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: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 = 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.


Re: How to use CXF and JSON

Posted by Sergey Beryozkin <sb...@progress.com>.
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 = 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.


RE: How to use CXF and JSON

Posted by "KARR, DAVID (ATTCINW)" <dk...@att.com>.
> -----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 = 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.


RE: How to use CXF and JSON

Posted by cgswtsu78 <cg...@proofpoint.com>.
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. 



@XmlRootElement(name="EnvFromHourlyWrapper")
public class EnvFromHourlyWrapper implements Serializable
{
	private static final long serialVersionUID = -1656397585968437184L;
	private List<EnvFromHourly> envs;

	public List<EnvFromHourly> getEnvs() {
		return envs;
	}

	public void setEnvs(List<EnvFromHourly> envs) {
		this.envs = envs;
	}

	
	
}




Dec 1, 2009 3:57:30 PM org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor
writeResponseErrorMessage
WARNING: No message body writer has been found for response class
EnvFromHourlyWrapper.




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: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.


RE: How to use CXF and JSON

Posted by cgswtsu78 <cg...@proofpoint.com>.
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: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-tp26600386p26601037.html
Sent from the cxf-user mailing list archive at Nabble.com.


RE: How to use CXF and JSON

Posted by "KARR, DAVID (ATTCINW)" <dk...@att.com>.
> -----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.


RE: How to use CXF and JSON

Posted by cgswtsu78 <cg...@proofpoint.com>.
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.  







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;
@Path("/chartsvcs/")
public class sample{
@POST	
@Path("/topspamsender")
@Produces({"application/xml", "application/json"})
public Response getTopSpamSenderData(ChartSpecification chartSpec) throws
Exception {	
	Response httpresp;
	String httpbody;	
	httpresp=null;		
	
	Object o = topSpamSenderService.getTopSpamSender(chartSpec); //just
retrieves an object to be returned to the consumer
		
	if (o != null)
	{
		httpbody = ObjToXmlStr(o, false);
		httpresp = setupResponse(Response.ok(httpbody)).build();									
	}
	else	
	{
		httpresp = setupResponse(Response.noContent()).build();			
	}
				
	return httpresp;

	}


/**
	 * Converts a Pojo to JAXB string
	 * @param obj
	 * @param includeXmlVerHdr  Indicates if returned string should be have the
XML version tag
	 * @return JAXB representation of the object
	 */
	private 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;
	}
}




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.


RE: How to use CXF and JSON

Posted by "KARR, DAVID (ATTCINW)" <dk...@att.com>.
> -----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"})