{#========================================== Spincast Jackson XML plugin ==========================================#} {% extends "../../layout.html" %} {% block sectionClasses %}plugins plugins-spincast-jackson-xml{% endblock %} {% block meta_title %}Plugins - Spincast Jackson XML{% endblock %} {% block meta_description %}Spincast Jackson XML plugin provides XML functionalities using Jackson.{% endblock %} {% block scripts %} {% endblock %} {% block body %}

Overview

The Spincast Jackson XML plugin provides XML functionalities using Jackson. It contains an implementation of the IXmlManager interface.

Most of the time, the IXmlManager interface is used directly from the request context via the xml() method or indirectly via some methods on the response() add-on.

For example:

{% verbatim %}

public class AppController {

    public void myHandler(IDefaultRequestContext context) {

        // Create a Json object from a XML String, using the "xml()" add-on
        IJsonObject jsonObj = context.xml().fromXml("<user></user>");

        // Send an object as XML, using the "response()" add-on
        context.response().sendXml(jsonObj);
    }
}
{% endverbatim %}

You can also directly inject the IXmlManager instance where you need it in your application.

Installation

If you use the spincast-default artifact, this plugin is already installed so you have nothing more to do!

If you start from scratch using the spincast-core artifact, you can use the plugin by adding this artifact to your project:

<dependency>
    <groupId>org.spincast</groupId>
    <artifactId>spincast-plugins-jackson-xml</artifactId>
    <version>{{spincastCurrrentVersion}}</version>
</dependency>

You then install the plugin's Guice module, by passing it to the Guice.createInjector(...) method:

Injector guice = Guice.createInjector(
        new SpincastCoreGuiceModule(args),
        new SpincastJacksonXmlPluginGuiceModule(IAppRequestContext.class)
        // other modules...
        );

... or by using the install(...) method from your custom Guice module:

public class AppModule extends SpincastCoreGuiceModule {

    @Override
    protected void configure() {
        super.configure();
        install(new SpincastJacksonXmlPluginGuiceModule(getRequestContextType()));
        // other modules...
    }
    
    // ...
}

The IXmlManager interface

Methods :

  • String toXml(Object obj)
    Converts an object to XML.
    If the object to convert is a IJsonObject, its elements of type "IJsonArray" will have a "isArray='true'" attribute added. This way, the XML can be deserialized back to a IJsonObject correctly.
  • String toXml(Object obj, boolean pretty)
    Converts an object to XML.
    @param pretty If true, the generated XML will be formatted.
  • IJsonObject fromXml(String xml)
    Deserializes a XML to an IJsonObject. This will correctly manage the XML generated by toXml(), arrays included.
  • IJsonArray fromXmlToJsonArray(String xml)
    Deserializes a XML to an IJsonArray. This will correctly manage the XML generated by toXml(), arrays included.
  • <T> T fromXml(String xml, Class<T> clazz)
    Deserializes a XML to the given Class.
    Be aware that if you use a default Type like
    Map<String, Object>, the arrays will probably won't be deserialized correctly. Use the version returning a IJsonObject to get the arrays to work out of the box!
  • <T> T fromXmlToType(String xml, Type type)
    Deserializes a XML to the given Type.
    Be aware that if you use a default Type like
    Map<String, Object>, the arrays will probably won't be deserialized correctly. Use the version returning a IJsonObject to get the arrays to work out of the box!
  • <T> T fromXmlInputStream(InputStream inputStream, Class<T> clazz)
    Deserializes a XML inputstream to the given Type.
    Be aware that if you use a default Type like
    Map<String, Object>, the arrays will probably won't be deserialized correctly. Use the version returning a IJsonObject to get the arrays to work out of the box!

{% endblock %}