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