You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Raphaël Flores <rf...@moulon.inra.fr> on 2009/09/01 19:26:03 UTC

Formatting binding output (Json & Xml)

Hello,

I just wonder a thing, is there a property in CXF allowing to 
auto-indent output of JAXB binding (either XML or JSON) ?

I know that JAXB allows this with following code:
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

Does CXF has a property to store for indentation? And is it possible to 
programmatically configure that?

Thanks.

-- 
Raphaël Flores


Re: Formatting binding output (Json & Xml)

Posted by Sergey Beryozkin <se...@iona.com>.
Hi,

> marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
> Does CXF has a property to store for indentation? 

Yes, JAXBElementProvider supports the Marshaller.JAXB_FORMATTED_OUTPUT
property.

> And is it possible to programmatically configure that?

There's a couple of options for doing it programmatically. You can either
register an instance of org.apache.cxf.jaxrs.provider.JAXBElementProvider
(with JAXRSServerFactoryBean) and set a marshallProperties map property on
it. Another option is to register an instance of CXF out interceptor (again
with JAXRSServerFactoryBean) which will set this property on a message :

public class JAXBOutInterceptor extends AbstractOutDatabindingInterceptor {
    
    public JAXRSOutInterceptor() {
        super(Phase.MARSHAL);
    }

    public void handleMessage(Message message) {
        
              message.put(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    }
}
 
it is also possible to register an instance of CXF JAXB DataBinding with
JAXRSServerFactoryBean but the options described above should be enough...

Now about JSON. Jettison has had a patch submitted some time ago but that
patch was dealing with final strings so at a time it was not applied. I've
just chatted a bit with Dejan. There's a good chance we'll see a Jettison
1.2 released in time for CXF 2.3 which will have fixes for a number of open
issues, including the pretty-printing, support for streaming, as well as a
number of other issues which have been talked about at the users list
recently

cheers, Sergey


Raphaël Flores-2 wrote:
> 
> Hello,
> 
> I just wonder a thing, is there a property in CXF allowing to 
> auto-indent output of JAXB binding (either XML or JSON) ?
> 
> I know that JAXB allows this with following code:
> marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
> 
> Does CXF has a property to store for indentation? And is it possible to 
> programmatically configure that?
> 
> Thanks.
> 
> -- 
> Raphaël Flores
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Formatting-binding-output-%28Json---Xml%29-tp25244580p25254374.html
Sent from the cxf-user mailing list archive at Nabble.com.