WSO2 EI Dataservice作为具有序列调用的Rest API

时间:2018-03-26 17:52:46

标签: rest cors wso2dss wso2ei dataservice

我是wso2的新手,目前我正在尝试将mongodb数据服务公开为REST API。我按照this教程将我的数据服务公开为Rest资源,如下所示:

<data>
  <config>...</config>
   <query id="count" useConfig="MongoDB">
      <expression>myCollection.count()</expression>
      <result outputType="json">{&#xd;"Documents": {&#xd;"Document": [&#xd;{&#xd;"Data": "$document"&#xd;}&#xd;]&#xd;}&#xd;}</result>
   </query>
   
   <operation name="count_op">
      <call-query href="count"/>
   </operation>
   
   <resource method="GET" path="users/count">
      <call-query href="count"/>
   </resource>
</data>

工作正常,但是当我尝试从我的angular2项目访问资源时,我偶然发现了CORS问题。我读了一些关于它的帖子,并认为我需要修改我的Rest API,如下所示:

<resource methods="OPTIONS" url-mapping="/*">
       <inSequence>
           <property action="set" name="HTTP_SC" scope="axis2"
               type="STRING" value="200"/>
           <property action="set" name="messageType" scope="axis2"
               type="STRING" value="application/json"/>
           <sequence key="rest_add_access_control_headers"/>
           <respond/>
       </inSequence>
       <outSequence/>
       <faultSequence>
           <sequence key="rest_add_access_control_headers"/>
           <respond/>
       </faultSequence>
   </resource>

我尝试将此代码与我的数据服务定义(上面的那个)集成,但我无法使其工作,这让我想知道作为REST资源公开的数据服务是否与Rest API相同?我似乎无法调用数据服务定义中的任何元素或其他介体。我需要为此创建一个实际的Rest API吗?如果是这样,我如何使用我的mongo dataservice作为我的Rest API的资源?

编辑:我在尝试访问该服务时从我的angular2项目获得的CORS错误是“请求资源上没有'Access-Control-Allow-Origin'标头。”这是另一个话题。但它导致我编辑我的数据服务,所以我编辑了我的数据服务资源元素并添加了一个新的:

   <resource method="GET" path="users/count">
   
      <inSequence>
         <property action="set" name="HTTP_SC" scope="axis2" type="STRING" value="200"/>
         <property action="set" name="messageType" scope="axis2" type="STRING" value="application/json"/>
         <sequence key="rest_add_access_control_headers"/>
         <respond/>
      </inSequence>
      <outSequence/>
      <faultSequence>
         <sequence key="rest_add_access_control_headers"/>
         <respond/>
      </faultSequence>
      <call-query href="count"/>
   </resource>

     <resource method="OPTIONS" path="users/count">
       <inSequence>
           <property action="set" name="HTTP_SC" scope="axis2"
               type="STRING" value="200"/>
           <property action="set" name="messageType" scope="axis2"
               type="STRING" value="application/json"/>
           <sequence key="rest_add_access_control_headers"/>
           <respond/>
       </inSequence>
       <outSequence/>
       <faultSequence>
           <sequence key="rest_add_access_control_headers"/>
           <respond/>
       </faultSequence>
   </resource>

rest_add_access_control_headers 序列如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<sequence name="rest_add_access_control_headers" xmlns="http://ws.apache.org/ns/synapse">
    <property name="Access-Control-Allow-Origin" scope="transport"
        type="STRING" value="*"/>
    <property name="Access-Control-Allow-Methods" scope="transport"
        type="STRING" value="GET,POST,PUT,DELETE,OPTIONS"/>
    <property name="Access-Control-Allow-Headers" scope="transport"
        type="STRING" value="origin, content-type, accept, Authorization"/>
</sequence>

第一个似乎没问题,但第二个使我的数据服务有问题,在managemenet控制台中出现此错误:

DS Code: UNKNOWN_ERROR Source Data Service:- Name: mongodb_dataservice Location: \MongoDB.dbs Description: N/A Default Namespace: http://ws.wso2.org/dataservice Nested Exception:- java.lang.NullPointerException at org.wso2.carbon.dataservices.core.DataServiceFactory.createDataService(DataServiceFactory.java:207) at org.wso2.carbon.dataservices.core.DBDeployer.createDBService(DBDeployer.java:797) at org.wso2.carbon.dataservices.core.DBDeployer.processService(DBDeployer.java:1152) at org.wso2.carbon.dataservices.core.DBDeployer.deploy(DBDeployer.java:201) at org.apache.axis2.deployment.repository.util.DeploymentFileData.deploy(DeploymentFileData.java:136) at org.apache.axis2.deployment.DeploymentEngine.doDeploy(DeploymentEngine.java:807) at org.apache.axis2.deployment.repository.util.WSInfoList.update(WSInfoList.java:144) at org.apache.axis2.deployment.RepositoryListener.update(RepositoryListener.java:377) at org.apache.axis2.deployment.RepositoryListener.checkServices(RepositoryListener.java:254) at org.apache.axis2.deployment.RepositoryListener.startListener(RepositoryListener.java:371) at org.apache.axis2.deployment.scheduler.SchedulerTask.checkRepository(SchedulerTask.java:59) at org.apache.axis2.deployment.scheduler.SchedulerTask.run(SchedulerTask.java:67) at org.wso2.carbon.core.deployment.CarbonDeploymentSchedulerTask.runAxisDeployment(CarbonDeploymentSchedulerTask.java:93) at org.wso2.carbon.core.deployment.CarbonDeploymentSchedulerTask.run(CarbonDeploymentSchedulerTask.java:138) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.NullPointerException at org.wso2.carbon.dataservices.core.engine.DataService.init(DataService.java:352) at org.wso2.carbon.dataservices.core.DataServiceFactory.createDataService(DataServiceFactory.java:190) ... 20 more

同样,我不确定我是否应该将这些代码放在数据服务定义中,因为代码是用于Rest API定义的,你使用序列,调解器等等......

如何使用ESB API使用我的dataservice?请您提供一个如何做到这一点的例子?我做了一些研究但找不到任何东西。

1 个答案:

答案 0 :(得分:1)

我可以知道你为什么无法调用数据服务吗?调用时是否收到任何错误?如果你能提供日志,那将会很有帮助。 此外,DSS用于从数据库中获取数据,最佳做法是提供一个ESB API,它将使用此服务而不是直接向外界公开DSS服务