Hi,
BadgerFishXMLOutputFactory() is the one which creates JSON-aware writers...
cheers, Sergey
Raphaël Flores-2 wrote:
>
> Sergey,
>
> You said this provider writes JSON (and so produces it), but I don't see
> where JSON is engaged in this code. Because I only use XMLOutputFactory
> and XMLStreamWriter, nothing apparently in relation with JSON.
>
> Could you explain it please ?
>
> Thanks, Raphaël.
>
>
> Sergey Beryozkin a écrit :
>> Hi
>>
>> Well, your custom provider says that it produces application/xml but
>> actually writes JSON hence JSON is produced.
>>
>> I'm not quite sure why you've decided to do a custom provider. Just
>> having
>>
>> @GET
>> @Path("/projects/{id}")
>> @Produces("application/xml")
>> public Project getProject(@PathParam("id") int id, @Context
>> HttpHeaders headers) {}
>>
>> should produce XML, there's a default JAXB provider available. Later on
>> you
>> can update @Produces and add application/json to it....
>>
>> cheers, Sergey
>>
>>
>> Raphaël Flores-2 wrote:
>>
>>> Hello all.
>>>
>>> With CXF, I'd like to map my java objects to XML (and later to Json) in
>>> order to send the (streaming) XML to a client. This client might use
>>> JAXB, without any CXF dependances. Currently, I don't use WSDL either
>>> Spring, just CXF HTTP annotation with a web.xml servlet for the
>>> deployment, simply.
>>>
>>> I've taken exemple on BadgerFishProvider to construct the provider I
>>> need for my app, the code is below:
>>>
>>> ============================================================
>>> @Produces("application/xml")
>>> @Consumes("application/xml")
>>> @Provider
>>> public final class BadgerFishProvider implements
>>> MessageBodyWriter<ServerProject> {
>>>
>>> private static Map<Class, JAXBContext> jaxbContexts = new
>>> WeakHashMap<Class, JAXBContext>();
>>> @Context private HttpHeaders requestHeaders;
>>>
>>> public long getSize(ServerProject project, Class<?> type, Type
>>> genericType, Annotation[] annotations, MediaType m) {
>>> return -1; }
>>>
>>> public boolean isWriteable(Class<?> type, Type genericType,
>>> Annotation[] annotations, MediaType m) {
>>> return type.getAnnotation(XmlRootElement.class) != null; }
>>>
>>> public void writeTo(ServerProject project, Class<?> clazz, Type
>>> genericType, Annotation[] annotations, MediaType m,
>>> MultivaluedMap<String, Object> headers, OutputStream os)
>>> throws IOException, WebApplicationException {
>>> try {
>>> JAXBContext context = getJAXBContext(project.getClass());
>>> Marshaller marshaller = context.createMarshaller();
>>>
>>> XMLOutputFactory factory = new BadgerFishXMLOutputFactory();
>>> XMLStreamWriter xsw = factory.createXMLStreamWriter(os);
>>> marshaller.marshal(project, xsw);
>>> xsw.close(); }
>>> catch (JAXBException e) { e.printStackTrace();
>>> }
>>> catch (XMLStreamException e) { e.printStackTrace();
>>> }
>>> }
>>>
>>> private JAXBContext getJAXBContext(Class type) throws JAXBException
>>> {
>>> synchronized (jaxbContexts) {
>>> JAXBContext context = jaxbContexts.get(type);
>>> if (context == null) {
>>> context = JAXBContext.newInstance(type);
>>> jaxbContexts.put(type, context);
>>> } return context; }
>>> }
>>> }
>>> ============================================================
>>>
>>> The ServerProject class right has the @XmlRootElement(name =
>>> "ServerProject") tag and it extends a class Project which contains all
>>> attributes and getters and setters.
>>>
>>> Here is the method used at frontend :
>>>
>>> ============================================================
>>> @GET
>>> @Path("/projects/{id}")
>>> @Produces("application/xml")
>>> public Project getProject(@PathParam("id") int id, @Context
>>> HttpHeaders headers) throws Base64Exception, IOException {
>>> ...
>>> try {
>>> Project project = factory.getProject(user, id);
>>> return project;
>>> }
>>> ...
>>> }
>>> ============================================================
>>>
>>> The problem is that marshalled data is JSON instead of being XML.Here is
>>> the result sent by my app:
>>>
>>> {"ServerProject":{"dbId":{"$":"128"},"logId":{"$":"0"},"projectId":{"$":"128"},"private":{"$":"false"},"projectDescription":{"$":""},"projectName":{"$":"MAIZEGS"},"remark":{"$":""}}}
>>>
>>>
>>> I don't understand why Json is produced while all seems to expect XML
>>> data... Does anyone know why ? And if i'm wrong concerning the way I use
>>> to marshall my data with JaxB, do not hesitate to warn me, i'm not sure
>>> to have the good approach.
>>>
>>> Thanks for your attention.
>>>
>
> --
> Raphaël Flores
> Téléphone : 01 69 33 23 76
> UMR de Génétique Végétale, INRA, Univ. Paris-Sud,
> CNRS, AgroParisTech, F-91190 Gif-sur-Yvette, France
>
>
>
--
View this message in context:
http://www.nabble.com/Getting-JSON-instead-of-XML-at-marshalling-step-tp25076341p25083376.html
Sent from the cxf-user mailing list archive at Nabble.com.