How to process JSON/XML string in SAP Cloud Applications Studio? – Part 1


Written by Kristián Kačinetz, SAP Developer & Ondrej Rimovský, SAP Developer

A request for creating the integration for third-party applications in currently very common. The interconnection of multiple solutions and maximizing the added value for the client is the task of many software solution providers. We are no exception.
As we focus on CX solutions, a large set of our focus is made up of SAP systems. Cloud solutions are currently coming to the forefront very significantly. Of course, SAP also has a whole range of cloud solutions. This portfolio of SAP cloud solutions is called SAP C4HANA. We believe that the name SAP C4HANA is not a new concept for you, but even if it was, it doesn’t matter, you can learn more about this topic in our Anodius articles.

This portfolio also includes the SAP Sales/Service Cloud solution (former SAP C4C), which we integrated with a third-party solution. A third party has provided us with a RESTful API to perform the necessary operations. A seemingly flawless scenario wasn’t that flawless at all.

As SAP Sales/Service Cloud doesn’t support native response processing from REST web services (using 
SAP Cloud Application Studio), we decided to process these REST services in Java and “transform” it into a SOAP service. Responses of that SOAP service can be mapped directly to the objects described by this web service in the SAP Sales/Service Cloud solution. Subsequent processing is therefore relatively well implementable within ABSL methods. We are bringing you the first of a series of articles focused on the topic – web services and their use/processing in SAP Sales/Service Cloud. 


You can download the source files for the project at the end of this article. 


First of all, it is necessary to choose a suitable IDE for working with Java projects, as in this environment, we will continue to work and create the mentioned SOAP web service. Based on our positive experience, we chose the IntelliJ development environment from JetBrains. You can download IntelliJ as a free Community version directly from 
the producer’s website.

We will use the Maven project as it allows us to create web applications easily as well as to generate SOAP web services. We will use the SAP Cloud Platform to run the application itself. Within this platform, it is possible to use the dedicated Java server and deploy Java applications compressed into a .war file.
It is also possible to use our Tomcat server to run the application; we will use the possibility of running the application on the SAP Cloud Platform for demonstration purposes.

Downloads

How to process JSON/XML string in SAP Cloud Applications Studio – Part 1

Creating the Maven project

We create a new project in IntelliJ. Select Maven, click Create from archetype, and search for the archetype web app, as illustrated in the following image:

SAP Cloud Applications Studio

In the following steps, we are prompted to select a project name, leaving the remaining settings as they are. IDE generates the project structure. In the root directory, we find the file pom.xml, the content of which is listed below:

 <?xml version="1.0" encoding="UTF-8"?>

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0 </modelVersion>

   <groupId>org.anodius.soap.guide </groupId>
   <artifactId>soap_guide </artifactId>
   <version>1.0-SNAPSHOT </version>
   <packaging>war </packaging>

   <name>First SOAP WebService </name>
   <url>https://www.anodius.com </url>

   <properties>
     <project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
     <maven.compiler.source>1.7 </maven.compiler.source>
     <maven.compiler.target>1.7 </maven.compiler.target>
   </properties>

   <dependencies>
     <dependency>
       <groupId>junit </groupId>
       <artifactId>junit </artifactId>
       <version>4.13 </version>
       <scope>test </scope>
     </dependency>
     <dependency>
       <groupId>com.sun.xml.ws </groupId>
       <artifactId>jaxws-rt </artifactId>
       <version>2.2 </version>
     </dependency>
     <dependency>
       <groupId>com.sun.istack </groupId>
       <artifactId>istack-commons-runtime </artifactId>
       <version>3.0.8 </version>
     </dependency>
   </dependencies>

   <build>
     <finalName>soap_guide </finalName>
     <pluginManagement> <!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
       <plugins>
         <plugin>
           <artifactId>maven-clean-plugin </artifactId>
           <version>3.1.0 </version>
         </plugin>
         <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
         <plugin>
           <artifactId>maven-resources-plugin </artifactId>
           <version>3.0.2 </version>
         </plugin>
         <plugin>
           <artifactId>maven-compiler-plugin </artifactId>
           <version>3.8.0 </version>
         </plugin>
         <plugin>
           <artifactId>maven-surefire-plugin </artifactId>
           <version>2.22.1 </version>
         </plugin>
         <plugin>
           <artifactId>maven-war-plugin </artifactId>
           <version>3.2.2 </version>
         </plugin>
         <plugin>
           <artifactId>maven-install-plugin </artifactId>
           <version>2.5.2 </version>
         </plugin>
         <plugin>
           <artifactId>maven-deploy-plugin </artifactId>
           <version>2.8.2 </version>
         </plugin>
       </plugins>
     </pluginManagement>
     <plugins>
       <plugin>
         <groupId>org.apache.maven.plugins </groupId>
         <artifactId>maven-compiler-plugin </artifactId>
         <configuration>
           <source>8 </source>
           <target>8 </target>
         </configuration>
       </plugin>
     </plugins>
   </build>
 </project>


At the end of this section, we set up an SDK. We get to the settings with the keyboard shortcut CTRL + ALT + SHIFT + S. We go to the
Platform Settings section where we click on SDKs. At the top of the window, we click on the plus icon and select the JDK download option.
We choose Oracle OpenJDK from the list; the environment offers us the current version of JDK and the location in which it will be saved. Then we go back to the
Project Settings section, click on the project tab and select openjdk-{version} in the Project SDK.

Creating a SOAP service

In the main folder, we create a Java folder. We create two new packages as sub-directories of the Java folder: types and soap. One will we need for types and the other for SOAP services. After this step, our project should have the following structure:

SAP Cloud Applications Studio

In package types, we create a new Java class called UserName. In this class, we define two new private variables of the String type, namely Name and Surname. Then we right-click somewhere in the editor space, and we select Generate (can also be called via the keyboard shortcut ALT + Insert) and let the Getter and Setter methods be generated:
SAP Cloud Applications Studio

After having generated the Get and Set methods successfully, the code for our UserName class should look as follows:


package types;

public class UserName {
    private String Name, Surname;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getSurname() {
        return Surname;
    }

    public void setSurname(String surname) {
        Surname = surname;
    }
}


Within package types, we create another class, which will be used to return a collection of type UserName in our SOAP service. We call this class UserNames. In this class we define one private variable UserNames, which will be of type List <UserName>, and then we proceed the same as in the case of the first class and we get the Getter and Setter methods.


As a result, the code for our UserNames class should look as follows:


package types;

import java.util.List;

public class UserNames {
    private List UserNames;

    public List getUserNames() {
        return UserNames;
    }

    public void setUserNames(List userNames) {
        UserNames = userNames;
    }
}


In the soap, the package will create a new class and name it, for example, GuideService. In this class, the operations (methods) of our SOAP service will be defined.


The code for this method will be as follows:

package soap;

import types.*;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import java.util.ArrayList;
import java.util.List;

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class GuideService
{
    @WebMethod(operationName = "sayHello")
    public String sayHello(@WebParam(name="guestname") String guestname) {
        if (guestname == null) { return "Hello"; }
        return "Hello " + guestname;
    }

    @WebMethod(operationName = "printNames")
    public UserNames printNames() {
        UserNames usrNames = new UserNames();
        List usrName = new ArrayList<>();
        UserName usr1 = new UserName();
        UserName usr2 = new UserName();
        usr1.setName("John");
        usr1.setSurname("Doe");
        usr2.setName("Andrew");
        usr2.setSurname("Eod");
        usrName.add(usr1);
        usrName.add(usr2);
        usrNames.setUserNames(usrName);
        return usrNames;
    }
}


To give the
jaxws framework an instruction that we have a service that responds to any endpoint provided, we need to create a sun-jaxws.xml file. We create this file in the src/main/webapp/WEB-INF folder.


The contents of this file should look as follows:

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
           version="2.0">
    <endpoint name="GuideService"
              implementation="soap.GuideService"
              url-pattern="/guideService" >
    </endpoint>
</endpoints>


There is also a web.xml file in the same folder, the contents of which we can modify as follows:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <listener>
    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>guideService</servlet-name>
    <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>guideService</servlet-name>
    <url-pattern>/guideService</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>120</session-timeout>
  </session-config>
</web-app>


If we want the home page to contain a link to our SOAP service, we edit the 
index.jsp file in the src/main/webapp/ folder with the content listed below:

<html>
	<body>
		<h2>SOAP Service is available <a href="./guideService">here</a>!</h2>
	</body>
</html>


Creating a .war file

If you are using IntelliJ IDEA in the Ultimate version, it is enough to click on Build Archetypes in the build and select all of them. In our case, when using the IntelliJ IDEA Community version, we must first download and set up Maven. Use the Maven website to download the package in .zip archive. We will unpack the archive into our user folder:  

  C:\Users\{user_name} \.maven

 

 

We will also download Oracle OpenJDK, which should be located in the user folder when selecting a predefined folder:

  C:\Users\{user_name} \.jdks\openjdk-{version}

 

 

In Window we define two new system variables JAVA_HOME and MAVEN. The variables are defined in the environment variables.


In the user variables, we edit the 
Path variable and add two new records, as illustrated in the following figure:

SAP Cloud Applications Studio

Then we open the command line and test the functionality of Maven by entering the command “mvn -v“.

Then we navigate to the root directory of our project on the command line. Run the command in this directory:

mvn package

 

 

We should see the following output on the command line:
SAP Cloud Applications Studio

A folder named target will be created in the root folder, which contains our .war file:
SAP Cloud Applications Studio

Upload the application to the SAP Cloud Platform

The last step is uploading the application to the SAP Cloud Platform. After logging in to the SAP Cloud Platform, we switch to the Neo environment. In the left main navigation bar, click on Applications and select Java Applications. In the upper left corner of the main part of the website, we click on the Deploy Application button; then we will see a form that we fill out as illustrated in the image below:
SAP Cloud Applications Studio

Click the Deploy button at the bottom of the form and click the Start button after uploading the application:
SAP Cloud Applications Studio

We click on the link that redirects us to more detailed information about the application:
SAP Cloud Applications Studio

In the Application URLs section, click on the link, and we will be redirected to the next page:
SAP Cloud Applications Studio

After clicking on the link, this time we will be redirected to the main page of our SOAP service:
SAP Cloud Applications Studio

We can download WSDL of our service, WSLD also contains a link to the XSD schema. The service can be tested with tools such as SoapUI or Postman:
SAP Cloud Applications Studio

In the output, it is possible to see that the answer contains two entities that we have defined in our Java project. 

 

To be Continued…

In the next part of this series of articles, we will show how it is possible to extend our SOAP service by processing responses from REST web services in JSON format.

  

 

 

Stay tuned to #AND!

Sign-up for newsletter