Elia
Elia

Reputation: 1443

WSO2 AS how to customize json provider

I have a JAX-RS Web App created by wso2 Studio. In my app I produce a json response. The JSON provider should be included in the cfx library. From cfx documentation I read that I can personalize my provider for delete RootElement from my JSON output.

Follow the documentation I add this bean in cfx-servelt.xml

<bean class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
    <property name="dropRootElement" value="true"/>
    <property name="dropCollectionWrapperElement" value="true"/>
    <property name="serializeAsArray" value="true"/>
    <property name="supportUnwrapped" value="true"/>
</bean>

Unfortunately the rootElement was not remove and no errors is generate. Where is the mistacke?

Thank you!

Upvotes: 1

Views: 275

Answers (1)

Diego Delgado
Diego Delgado

Reputation: 87

I'm using WSO2 Developer Studio 3.8.0 and WSO2 AS 5.2.1. This is my cxf-servlet.xml and it works as expected:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
    <bean  id="MyServiceBean" class="my.service.class"/>
    <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
        <property name="dropRootElement" value="true"/>
        <property name="supportUnwrapped" value="true"/>
    </bean>
    <jaxrs:server id="MyService" address="/myServiceURL">
        <jaxrs:serviceBeans>
            <ref  bean="MyServiceBean"/>
        </jaxrs:serviceBeans>

        <jaxrs:providers>
            <ref bean="jsonProvider" />
        </jaxrs:providers>
    </jaxrs:server>
</beans>

I have the following API method and return type:

@GET
@Path("/checkEmail/{username}")
@Produces("application/json")
public CheckEmailResponse checkEmail(@PathParam("username") String username) throws Exception {
}

@XmlRootElement
public class CheckEmailResponse {
    public boolean exists;
    public boolean success;
}

And, as expected, the returned JSON is unwrapped:

{"exists":true,"success":true}

The same for the any JSON input parameter, e.g.:

{
  "username": "user",
  "serviceProvider": "sp"
}

I think the dropRootElement property manages the return parameters, and the supportUnwrapped property manages the input parameters.

Upvotes: 1

Related Questions