cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

consume data from java api to sapui5

2,535

Hello,
I have built a java API(deployed on SAP Cloud Platform) on and want to display the acquired JSON data to sapui5.
Is there any way that I can call the java API for example, via URL?

Or is it necessary to use OData service?
If the answer is latter, could you guide me how it works or provide some documents for beginner.
I have read the blog https://blogs.sap.com/2016/02/08/odata-everything-that-you-need-to-know-part-1/
but I would like to know what would be the relationship among sapui5, java api and OData service.


I have also read the post below which is similar to my questions but could not figure out how the problem is solved.https://answers.sap.com/questions/10953428/consume-java-api-from-a-sapui5-application.html

Any help would be appreciate.

Best regards

Roan

Accepted Solutions (1)

Accepted Solutions (1)

Ivan-Mirisola
Product and Topic Expert
Product and Topic Expert

Hi Yuning Roan,

To make a call to a REST or OData service from a SAPUI5 app you should be using a destination defined in the cockpit to create an abstraction layer for your code (to avoid hard-coding any end-points). To make a rest or odata call you can use the JSONModel or ODataModel, like it is described below:

https://sapui5.hana.ondemand.com/1.34.7/docs/guide/44062441f3bd4c67a4f665ae362d1109.html

Notice that you have to define the destination created in the cockpit inside your neo-app.json and manifest.json (this is described on the link above).

The below blog describes how to use the odata model. But for REST services the JSON model can be used in a similar way.

https://blogs.sap.com/2017/03/13/how-to-consume-an-odata-service-with-openui5-sapui5/

Furthermore, an API usually is protected by an API Key in which you could request authentication via OAuth method. In such case, you shouldn't make a call directly from browser a based application such as sapui5. The reason is that you will incur in exposing the API Key on the browser. So, anyone that has access to the source code of the application will be able to see the API Key unprotected.

The solution is to make the API call via a protected Java Application that implements an OData or REST service or even use the OAuth2SAMLAuthentication in a destination for your sapui5 app.

Best regards,
Ivan

0 Likes

Hi Ivan,

Thank you for the clear instruction.

Here are some more detailed background of what I am trying to achieve.
SAPUI5: connect to JavaAPI deployed on SCP as Java Application and display the data to UI.
JavaAPI: call SFSF OData API using HTTP Methods and filter the data as JSON to be displayed.

Now I got some other questions after reading the references you provided.
As for the CORS issues, the first reference(https://sapui5.hana.ondemand.com/1.34.7/docs/guide/44062441f3bd4c67a4f665ae362d1109.html) provided a solution: Use a helper service from the same domain of our app as a proxy to call the real remote service.
And providing Northwind Destination as an example.


Am I able to set the Java Application domain as Destination?
If yes, I tried to set one but failed.(as below) Could you please guide me to the correct setting.
If no, what would be another solution.



neo.json

{
    "routes": [
	{
	  "path": "/destinations/javaApi",
	  "target": {
          "type": "destination",
          "name": "javaApi"
        },
          "description": "javaApi"
	}
    ]
}


manifest.json

"dataSources": {
	"javaApi": {
		"uri": "/destinations/javaApi/",
		"type": "JSON"
			}
		}
}



I have tried to set CORS response headers in java api(remote system) and use jQuery ajax to get the JSON data. This successfully display data on SAPUI5. However, in this way I have only coming out setting absolute URL and encountering hard-coding issue you mentioned. So I am wondering if there is a better solution.

Please let me know if I have misunderstood anything.
I really appreciate your kindly help.

Best regards,

Roan

Ivan-Mirisola
Product and Topic Expert
Product and Topic Expert
0 Likes

Hi Yuning Roan,

Yes you can. Every app on SCP requires to be authentication. So, you need to use the destination with ApptoApp authentication.

You then have to create and use the JSON model in your SAPUI5 to call the destination.

Best regards,
Ivan

Answers (2)

Answers (2)

former_member273190
Participant
0 Likes

Hi Ivan,

Let's say i need to get data from a public API but i don't need to expose the whole URL directly on javascript code in my app and also for security purposes because i won't use SCP, just on premise and the custom app into Fiori launchpad.

In this case, is it possible to configure into neo-app.json and manifest.json and use it without SCP?

BR,

Maurício

ankit12
Active Participant
0 Likes

Hi Yuning Roan,

could you please tell that java API is on internet or is it on premise API you are trying to consume to SAP UI5?

if Java API is on internet then it should not be difficult.

but if you are trying to consume On premise API to SAP UI5 then you need Cloud connector to expose the service first. for more details you can refer this blog.

rest-service-using-sap-cloud-connector-with-sap-cloud-platform/

Regards,

Ankit Gupta

0 Likes

Hi Ankit Gupta,

Thank you for the instruction.
My java API is on the Internet.
However, I might consume on premise API one day in the future so thank you for the information.

Regards,
Yuning Roan