wso2 Data Services REST POST JSON format

787 views Asked by At

I am trying (real hard) to make a data service in WSO2 EI611 that I can call in JSON. But I have no idea of the JSON format to use when I post my data to the service. Tried a lot of format but I get Null Error.

Thanks in advance.

Here is my data service code :

<data name="OAuthToken" serviceGroup="" serviceNamespace="">
        <description/>
        <config id="QT_DB">
        <property name="org.wso2.ws.dataservice.user">wso2</property>
        <property name="org.wso2.ws.dataservice.password">wso2</property>
        <property name="org.wso2.ws.dataservice.protocol">jdbc:mysql://192.168.1.10:3306/QT_DB</property>
        <property name="org.wso2.ws.dataservice.driver">com.mysql.jdbc.Driver</property>
        </config>
        <query id="AddNewToken" useConfig="QT_DB">
        <sql>INSERT INTO QT_DB.OAUTH_TOKEN (ACCESS_TOKEN, TOKEN_TYPE, API_SERVER, CREATED, EXPIRES_IN, EXPIRATION, REFRESH_TOKEN) VALUES(:ACCESS_TOKEN, :TOKEN_TYPE, :API_SERVER, :CREATED, :EXPIRES_IN, DATE_ADD(:CREATED, INTERVAL :EXPIRES_IN SECOND), :REFRESH_TOKEN);
     </sql>
        <param name="ACCESS_TOKEN" sqlType="STRING" type="IN"/>
        <param name="TOKEN_TYPE" sqlType="STRING" type="IN"/>
        <param name="API_SERVER" sqlType="STRING" type="IN"/>
        <param name="CREATED" sqlType="TIMESTAMP" type="IN"/>
        <param name="EXPIRES_IN" sqlType="INTEGER" type="IN"/>
        <param defaultValue="0" name="EXPIRATION" sqlType="INTEGER" type="IN"/>
        <param name="REFRESH_TOKEN" sqlType="STRING" type="IN"/>
        </query>
        <query id="GetLastToken" useConfig="QT_DB">
        <sql>SELECT CREATED,ACCESS_TOKEN, TOKEN_TYPE, API_SERVER, EXPIRES_IN, EXPIRATION, REFRESH_TOKEN FROM OAUTH_TOKEN WHERE CREATED = (SELECT MAX(CREATED) FROM OAUTH_TOKEN);</sql>
        <result defaultNamespace="" element="OAUTH_TOKENS" rowName="OAUTH_TOKEN">
            <element column="ACCESS_TOKEN" name="ACCESS_TOKEN" xsdType="xs:string"/>
            <element column="TOKEN_TYPE" name="TOKEN_TYPE" xsdType="xs:string"/>
            <element column="API_SERVER" name="API_SERVER" xsdType="xs:string"/>
            <element column="CREATED" name="CREATED" xsdType="xs:dateTime"/>
            <element column="EXPIRES_IN" name="EXPIRES_IN" xsdType="xs:integer"/>
            <element column="EXPIRATION" name="EXPIRATION" xsdType="xs:dateTime"/>
            <element column="REFRESH_TOKEN" name="REFRESH_TOKEN" xsdType="xs:string"/>
        </result>
        </query>
        <operation returnRequestStatus="true" name="GetLastToken">
        <call-query href="GetLastToken"/>
        </operation>
        <operation returnRequestStatus="true" name="AddNewToken">
        <call-query href="AddNewToken">
            <with-param name="ACCESS_TOKEN" query-param="ACCESS_TOKEN"/>
            <with-param name="TOKEN_TYPE" query-param="TOKEN_TYPE"/>
            <with-param name="API_SERVER" query-param="API_SERVER"/>
            <with-param name="CREATED" query-param="CREATED"/>
            <with-param name="EXPIRES_IN" query-param="EXPIRES_IN"/>
            <with-param name="REFRESH_TOKEN" query-param="REFRESH_TOKEN"/>
        </call-query>
        </operation>
        <resource method="GET" path="GetLastToken" returnRequestStatus="true">
        <call-query href="GetLastToken"/>
        </resource>
        <resource method="POST" path="AddNewToken" returnRequestStatus="true">
        <call-query href="AddNewToken">
            <with-param name="ACCESS_TOKEN" query-param="ACCESS_TOKEN"/>
            <with-param name="TOKEN_TYPE" query-param="TOKEN_TYPE"/>
            <with-param name="API_SERVER" query-param="API_SERVER"/>
            <with-param name="CREATED" query-param="CREATED"/>
            <with-param name="EXPIRES_IN" query-param="EXPIRES_IN"/>
            <with-param name="REFRESH_TOKEN" query-param="REFRESH_TOKEN"/>
        </call-query>
        </resource>
        <resource method="POST" path="AddNewToken2" returnRequestStatus="true">
        <call-query href="AddToken">
            <with-param name="ACCESS_TOKEN" query-param="ACCESS_TOKEN"/>
            <with-param name="TOKEN_TYPE" query-param="TOKEN_TYPE"/>
            <with-param name="API_SERVER" query-param="API_SERVER"/>
            <with-param name="CREATED" query-param="CREATED"/>
            <with-param name="EXPIRES_IN" query-param="EXPIRES_IN"/>
            <with-param name="EXPIRATION" query-param="EXPIRATION"/>
            <with-param name="REFRESH_TOKEN" query-param="REFRESH_TOKEN"/>
        </call-query>
        </resource>
        <query id="AddToken" useConfig="QT_DB">
        <sql>INSERT INTO QT_DB.OAUTH_TOKEN (ACCESS_TOKEN, TOKEN_TYPE, API_SERVER, CREATED, EXPIRES_IN, EXPIRATION, REFRESH_TOKEN) VALUES(:ACCESS_TOKEN, :TOKEN_TYPE, :API_SERVER, :CREATED, :EXPIRES_IN, :EXPIRATION, :REFRESH_TOKEN);</sql>
        <param name="ACCESS_TOKEN" sqlType="STRING" type="IN"/>
        <param name="TOKEN_TYPE" sqlType="STRING" type="IN"/>
        <param name="API_SERVER" sqlType="STRING" type="IN"/>
        <param name="CREATED" sqlType="TIMESTAMP" type="IN"/>
        <param name="EXPIRES_IN" sqlType="INTEGER" type="IN"/>
        <param defaultValue="0" name="EXPIRATION" sqlType="TIMESTAMP" type="IN"/>
        <param name="REFRESH_TOKEN" sqlType="STRING" type="IN"/>
        </query>
    </data>

Here is the POST I Try to do :

{
    "AddNewToken2": {
        "ACCESS_TOKEN": "2GQL1fo_bHv9GwIALgMhF5cuxynFrbHC0",
        "TOKEN_TYPE": "Bearer",
        "API_SERVER": "https://api04.iq.questrade.com/",
        "CREATED": "2017-09-10T15:05:21.000-04:00",
        "EXPIRES_IN": "1800",
        "EXPIRATION": "2017-09-10T15:05:21.000-04:00",
        "REFRESH_TOKEN": "bz8O65CN9HYA2ripBhV9M5q7He65Ebls0"
    }
}

And the error I get :

[2017-09-10 22:59:07,433] [EI-Core] ERROR - ServerWorker Error processing POST reguest for : /services/OAuthToken.HTTPEndpoint/AddNewToken2. Error detail: null. 
java.lang.NullPointerException
    at org.apache.axis2.json.gson.GsonXMLStreamReader.validateArgumentTypes(GsonXMLStreamReader.java:783)
    at org.apache.axis2.json.gson.GsonXMLStreamReader.nextValue(GsonXMLStreamReader.java:734)
1

There are 1 answers

1
Sashika Wijesinghe On

When you send the POST request, the request payload should be as follows.

{
    "_postAddNewToken2": {
        "ACCESS_TOKEN": "2GQL1fo_bHv9GwIALgMhF5cuxynFrbHC0",
        "TOKEN_TYPE": "Bearer",
        "API_SERVER": "https://api04.iq.questrade.com/",
        "CREATED": "2017-09-10T15:05:21.000-04:00",
        "EXPIRES_IN": "1800",
        "EXPIRATION": "2017-09-10T15:05:21.000-04:00",
        "REFRESH_TOKEN": "bz8O65CN9HYA2ripBhV9M5q7He65Ebls0"
    }
}

Refer https://docs.wso2.com/display/EI611/JSON+Mapping+Sample for more information