cancel
Showing results for 
Search instead for 
Did you mean: 

MBO development for RESTFUL Webservices

Former Member
0 Kudos

Hi all,

  I am new to MBO development in SUP platform. I have some restful Webservices for reading, updating and deleting certain records. The restful webservice urls are

http://app2342.appspot.com/rest/insertservice/insert   [parameters are cuskey,cusname and method is POST]

http://app2342.appspot.com/rest/insertservice/update  [parameters are cuskey,cusname and method is POST]

http://app2342.appspot.com/rest/insertservice/delete  [parameters are cuskey and method is POST]

http://app2342.appspot.com/rest/hello/test  [for reading the records]

I want to know how to provide the input parameters for insert update and delete. Do I need to provide XSD files for request as well as response. Is there any tutorial available for MBO creation for Restful webservices. I could do the read successfully. If some one explains the steps for configuring this MBO, it will clear my fundamentals.

thanks and regards,

Accepted Solutions (0)

Answers (1)

Answers (1)

Dan_vL
Product and Topic Expert
Product and Topic Expert
0 Kudos

The following sample may be of some help. 

It is available at the SUP Apps site https://cw.sdn.sap.com/cw/groups/sup-apps

MBOs and Hybrid Apps https://cw.sdn.sap.com/cw/docs/DOC-152083

Specifically it has the following section.

Changing an MBO's Data Source from a Database Table to a REST Service

In this section an MBO's back-end data source will be modified from a database table to a REST web service. Note the hybrid app will not require any changes due to this change.


SQL Anywhere contains a built in HTTP web service that allows for the hosting of web services. In order to enable the web server, the following parameter needs to be added to the list of parameters used to start the database server that hosts the sample database.

-xs http(port=8082)

See SQL Anywhere as an HTTP Web Server for additional details.

The parameters that are used to start the sampledb SQL Anywhere service are stored in the registry as shown below.



The registry editor can be started via Start > Run... > regedit.


Next run services.msc to stop and restart the sampledb service.



If there is a problem restarting the service, check the log at

C:\Sybase\UnwiredPlatform\Servers\UnwiredServer\logs\errorlog-sampledb.txt

A period of a couple of minutes may need to pass before the service can be restarted.


In Hybrid_Apps_And_MBOs_Sample.sql, highlight the following line and choose Execute Selected Text.

CREATE SERVICE ListSalesOrderItems TYPE 'XML' AUTHORIZATION OFF USER DBA AS SELECT * from DBA.Sales_order_items;

The REST service can now be accessed in a browser via the URL

http://localhost:8082/sampledb/ListSalesOrderItems


Rename the Sales_order_items_Scheduled MBO to Sales_order_items_Scheduled_bak. Create a new connection in the enterprise explorer to the REST service by right clicking on REST Web Services and choosing New.

Provide a name such as ListSalesOrderItemsREST




Create a new MBO dragging and dropping the previously created REST service onto the mobile application diagram. Select Attributes as we wish to populate the attributes of the MBO from this service.




Change the HTTP method to be GET which is typically used for read operations in REST services. The next step is to provide an XSD file that describes the format of the results returned from calling the REST service. An easy way to do this is to copy and paste the output from calling it and use a tool that generates and XSD from a sample XML file. This site www.freeformatter.com provides such a service. Use the following as input.

<root> <row id="2001" line_id="1" prod_id="300" quantity="12" ship_date="1996-09-15"/> <row id="2654" line_id="1" prod_id="301" quantity="15" ship_date="1999-10-29"/> </root> 

This generates the following XSD.

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="root"> <xs:complexType> <xs:sequence> <xs:element name="row" maxOccurs="unbounded" minOccurs="0"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute type="xs:short" name="id" use="optional"/> <xs:attribute type="xs:byte" name="line_id" use="optional"/> <xs:attribute type="xs:int" name="prod_id" use="optional"/> <xs:attribute type="xs:byte" name="quantity" use="optional"/> <xs:attribute type="xs:date" name="ship_date" use="optional"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> 

Now modify the XSD a bit to set the type to be int (instead of byte and short) and remove the simpleContent and extension as shown below.

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="root"> <xs:complexType> <xs:sequence> <xs:element name="row" maxOccurs="unbounded" minOccurs="0"> <xs:complexType> <xs:attribute type="xs:int" name="id" use="optional"/> <xs:attribute type="xs:int" name="line_id" use="optional"/> <xs:attribute type="xs:int" name="prod_id" use="optional"/> <xs:attribute type="xs:int" name="quantity" use="optional"/> <xs:attribute type="xs:date" name="ship_date" use="optional"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> 

Create a file named ListSalesOrderItems.xsd with the above contents and copy it into the HybridAppsAndMBOs project. This file will be used to define what the output of calling this service will look like.



Don't forget to click Load Elements and select the root element.


The web service can be previewed to verify that everything is working.



Recreate the three object queries; findByLastMonth, findByLastYear and findByPeriod on the newly created MBO Sales_order_items_Scheduled.

SELECT x.* FROM Sales_order_items_Scheduled x WHERE x.ship_date > DATEADD(month, -1, '1998-11-26') and x.ship_date < '1998-11-26' ORDER BY x.ship_date; SELECT x.* FROM Sales_order_items_Scheduled x WHERE x.ship_date > DATEADD(year, -1, '1998-11-26') and x.ship_date < '1998-11-26' ORDER BY x.ship_date; SELECT x.* FROM Sales_order_items_Scheduled x WHERE x.ship_date >= :sDate AND x.ship_date <= :eDate ORDER BY x.ship_date; 

Note findByPeriod takes two date parameters sDate and eDate for start date and end date.


Finally, rename the created MBO to Sales_order_items_Scheduled, redeploy, and try it out.

Note that during the deployment of the MBO's select replace deployment mode. Also note that we did not have to redeploy or make any changes to our Hybrid App even though the back-end data source changed.



As an optional exercise, add a create operation to the newly created MBO. The following SQL can be used to create a REST service to perform the insert.

CREATE PROCEDURE CreateSalesOrderItemProc() BEGIN INSERT INTO sales_order_items(id, line_id, prod_id, quantity, ship_date) VALUES (http_variable('id'), http_variable('line_id'), http_variable('prod_id'), http_variable('quantity'), http_variable('ship_date')); SELECT 'Sales Order Item Added'; END;  CREATE SERVICE CreateSalesOrderItem TYPE 'RAW' AUTHORIZATION OFF USER DBA AS CALL CreateSalesOrderItemProc(); 

To test it out, perform a select against a sales order such as 2001 to see how many sales order items it has.

SELECT * FROM sales_order_items = 2001; 


Then use a browser to add a new row using the following URL.

http://localhost:8082/sampledb/CreateSalesOrderItem?id=2001&line_id=7&prod_id=300&quantity=24&ship_d...



Alternatively use a REST client such as the Advanced REST Client plugin for Chrome to send a POST request.



Create a new connection profile to the REST service as shown below.



/CreateSalesOrderItem?id={id(int)}&line_id={line_id(int)}&prod_id={prod_id(int)}&quantity={quantity(int)}&ship_date={ship_date(date)} 

Note that the format shown above is described further at Binding a REST Web Service Data Source to a Mobile Business Object

Then, drag and drop the connection profile to CreateSalesOrderItemsREST onto the operations section of the MBO.


Redeploy the MBO's in the project






Drag and drop the create operation from the Workspace Navigator onto the Flow Design tab of the QuerySalesOrders.xbw to create a create screen.


Deploy the hybrid app and try it out





Former Member
0 Kudos

Hi,

  Thanks for your help. I read through the steps and successfully did the create() operation using the get method. The resource base uri was http://app2342.appspot.com/rest/insertservice and the resource uri template was /insert?cuskey={cuskey}&cusname={cusname}. I deployed the MBO successfully and also tested it using the mobile client application. It is working perfectly allright.

  However now I want to use the POST method for the create. Can you please tell me the steps for that??. I tried on my own but it is failing in Test Execution. Request your kind help.

thanks and regards,