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