Hi Devs,

All the REST API methods which return collection types have included a
wrapper object in their response messages. This is according to the CXF
JSON provider configuration.

Example: GET Cartridges:

{"*cartridges*":[{"category":"framework","description":"Apache
Tomcat","displayName":"tomcat","host":"tomcat.apache.org
","multiTenant":false,"provider":"apache","public":false,"type":"tomcat","version":7}]}

This becomes a problem for the CLI or for any other client that try to
deserialize the JSON string to its corresponding Java object. The
convention we had used to resolve this issue was to define wrapper classes
for collection type responses:

    private class CartridgeList {
        private ArrayList<Cartridge> cartridges;

        public ArrayList<Cartridge> getCartridges() {
            return cartridges;
        }

        public void setCartridge(ArrayList<Cartridge> cartridges) {
            this.cartridges = cartridges;
        }

        CartridgeList() {
            cartridges = new ArrayList<Cartridge>();
        }
    }

IMO this is an overhead when considering the amount of classes we have in
the REST API domain model.

As a solution to this problem I have now configured REST API to remove this
element:

<bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider">

    ...
    <property name="dropCollectionWrapperElement" value="true"/>

    ...
</bean>


*New sample response:*

[{"category":"framework","description":"Apache
Tomcat","displayName":"tomcat","host":"tomcat.apache.org","multiTenant":false,"provider":"apache","public":false,"type":"tomcat","version":7}]

*Sample code to de-serialize the object:*

Type listType = new TypeToken<ArrayList<CartridgeDefinitionBean>>()
{}.getType();
List<CartridgeDefinitionBean> cartridgeList =
(List<CartridgeDefinitionBean>)
restClient.listEntity(ENDPOINT_LIST_CARTRIDGES,
        listType, "cartridges");


Dakshika: We might need to update the UI with this modification. Really
sorry for the inconvenience that may cause.

Thanks



-- 
Imesh Gunaratne

Technical Lead, WSO2
Committer & PMC Member, Apache Stratos

Reply via email to