REST Sample Project In SoapUI

In previous article we learned about REST Testing using SoapUI and continuing with REST testing, today we are covering how to create REST sample project in SoapUI.

JAX-RS Java API has two implementations known as

  1. Jersey.
  2. RESTEasy.

Jersey RESTful WebService: In this tutorial, we are going to develop and discuss on RESTful Web service using Jersey implementation of JAVA-RS Java API.

System Requirements:

  • Jersey jar files, which can be downloaded at below link.

https://jersey.java.net/download.html

  • Eclipse IDE.
  • Java Runtime Environment 1.7 and up.
  • Tomcat webserver 8.0 and up, integrated with eclipse.

Sample RESTful Web service project can be download from this link:

Download Sample REST Project In SoapUI

Project Development Steps:

Sample REST Project in SoapUI
Sample REST Project

Step 1: Make sure eclipse is running with integrated Java runtime environment and Apache Tomcat webserver 8.0.

Step 2: From Eclipse, right click at project explorer window and select New -> Dynamic Web Project. All skeleton director structure will be created as required to deploy on Tomcat Webserver. Give the project package name as “webservice-project-restful”.

Step 3: Within the project src directory add two more packages, one for server classes for Web Service and Client class to get the response from server. Name server side class package as ‘com.softwaretestingclass.kanif.server’ and client side package as ‘com.softwaretestingclass.kanif.client’.

Step 4: Add all the Jersey jars which were downloaded in the beginning of this tutorials into \WebContent\WEB-INF\lib directory.

Step 5: Create three classes in server package and one class in client package. Names of these classes as follows:

In Server Package,

  • AccountDetails.java
  • HTMLTextDemo.java
  • PlainTextDemo.java

In Client Package,

  • ClientTest.java

Screenshot for these classes as follows:

1. Account Details Class:

This class is like any other java class, in order to convert this class a RESTful web service we have added annotation as ‘@Path (“/account”)’. Java definitions for such annotations are present in Jersey jars which are present in WEB-INF/lib directory of this web project. By adding Path, this class service can obtained at this path by GET method which produces media type as XML. All these specifications are written in the form of annotations as ‘@GET’ and ‘@Produces (MediaType.TEXT_XML)’. Similarly annotation ‘@POST’ could be used for POST method of service and such method can either Produces or consume the request using annotation as ‘@Produces’  or ‘@Consumes’ respectively. We have created a method known as ‘sayHelloXML’ which returns the XML data as response when this Web service is called by Client system program. Java Class is demonstrated below in the program.

REST project account details

2. Plain Text Demo Class:

This class is like any other java class, in order to convert this class a RESTful web service we have added annotation as ‘@Path (“/plaintext”)’. Java definitions for such annotations are present in Jersey jars which are present in WEB-INF/lib directory of this web project. By adding Path, this class service can obtained at this path by GET method which produces media type as Plain text. All these specifications are written in the form of annotations as ‘@GET’ and ‘@Produces (MediaType.TEXT_PLAIN)’. Similarly annotation ‘@POST’ could be used for POST method of service and such method can either Produces or consume the request using annotation as ‘@Produces’  or ‘@Consumes’ respectively. We have created a method known as ‘sayHelloPlainText’ which returns the plain text data as response when this Web service is called by Client system program. Java Class is demonstrated below in the diagram.

REST Project Plain Text Demo

3. HTML Text Demo Class:

This class is like any other java class, in order to convert this class a RESTful web service we have added annotation as ‘@Path (“/htmltext”)’. Java definitions for such annotations are present in Jersey jars which are present in WEB-INF/lib directory of this web project. By adding Path, this class service can obtained at this path by GET method which produces media type as HTML text. All these specifications are written in the form of annotations as ‘@GET’ and ‘@Produces (MediaType.TEXT_HTML)’. Similarly annotation ‘@POST’ could be used for POST method of service and such method can either Produces or consume the request using annotation as ‘@Produces’  or ‘@Consumes’ respectively. We have created a method known as ‘sayHelloHTML’ which returns the HTML text data as response when this Web service is called by Client system program. Java Class is demonstrated below in the diagram.

REST Project HTML Home Page

4. Client Test Class:

This class resides at web server and able to access the web services class through their URL as demonstrated below in the JAVA class diagram. After accessing the root URL, in order to access plain test data, xml data and html data their corresponding full path is called to access the requested service. Here we are just printing the data on the console. Output is demonstrated in the end of this tutorial series.

REST Project Client Test

5: Other important resource of web dynamic project:

  1. Index.html
  2. Web.xml

Let’s discuss below index.html, which is specified as the welcome file in web.xml and it appears first when we run the entire project on Apache Tomcat Webserver. Here in index.html file, basic html tags are used to provide the hyperlink which looks up for the RESTful Web services classes and receives the response data as service.

REST project web_xml1

And this web.xml configuration is very important, let keep below steps in mind, in order our RESTful web services to work smoothly.

  • Add the servlet elements in web.xml as servlet-name ‘Jersey REST Service’ and give the class path as ‘org.glassfish.jersey.servlet.servletContainer’ in <servlet-class>. This is very important in order the ServletContainer servlet to work for JAVA-RS jars API.
  • Make sure to give the project server package name into parameter value as indicated below in the diagram.
  • We can map the URL pattern into web.xml, here we used /rest/*. Doing so our rest class URL will work as base URL + /rest/ + Web Service class URL.

REST project web_xml

6: Overall dynamic web project directory: 

Once all of above system setup, Java classes, index.html and web.xml files are ready, Overall dynamic web project directory structure will look as shown in the diagram below:

REST Jersey Project Hierarchy

7:

Now it is the time to build the web project and once compilation is successful either we can deploy this web project into Webserver or using eclipse directly Run As => Run on Server.

Output Summary:

We will see the below output at web URL http://localhost:8085/webservice-project-restful/ as follows:

Response check 1

To get the service as XML Response, click on the first hyperlink and below screen will be visible. It can also be accessed directly using the URL http://localhost:8085/webservice-project-restful/rest/account from any client on web.

Response check 3

To get the service as Plain text Response, click on the second hyperlink on the homepage screen and below screen will be visible. It can also be access directly using the URL http://localhost:8085/webservice-project-restful/rest/plaintext from any client on web.

Response check 2

To get the service as HTML text Response, click on the third hyperlink on home page screen, below screen will be visible. It can also be access directly using the URL http://localhost:8085/webservice-project-restful/rest/htmltext  from any client on web.

Response check 4

Remember, we also created a ClientTest class to print the response on the screen, here is the output when we run it as Java Application in eclipse.

ClientTestOutput

This complete the sample project on RESTful Web Service using JAVA-RS API (Jersey Jars). Now let’s discuss the Pros and Cons for REST Web service.

Pros and Cons of REST Web service:

Pros:

  • Unlike SOAP protocol, REST protocol based Web service supports other data formats as well. It overall supports XML, Plain Test, JSON and HTML.
  • REST Web Services are fast in operation as compared to SOAP Web services and consumes less bandwidth and resources.
  • REST Web Services are flexible and can use SOAP Web Services as the implementation.
  • Similar to SOAP Web Services, REST Web Services can be written in any programming language and can be executed in cross-platforms.

Cons:

  • None when compared with SOAP Web Service.

Comparison between SOAP vs REST Web services:

Properties SOAP Web service REST Web service
Protocol used SOAP Protocol based. REST Protocol based.
Full form Simple Object Access Protocol. Representational State Transfer.
Implementation & Use SOAP Web service cannot implement REST Web Services. REST Web Service can use and implement SOAP web services as it is concept that supports other protocols like HTTP, SOAP, etc.
URI and Interfaces SOAP uses services interfaces to use business logics. Do not use URIs. REST uses URI to use business logics.
JAVA API for Web Services JAX-WS is JAVA API. JAX-RS is JAVA API.
Standard definitions SOAP has many defined standards that are required to be obeyed. Unlike SOAP, REST does not have many standards and is very flexible to use.
Bandwidth and Resources Uses more bandwidth and resources for its operation. Uses less bandwidth and resources for its operation.
Preference It is less preferred due to lots of standards to be obeyed. It is more preferred over SOAP Web services as it has minimal standards and flexible in use and supports many data formats.
Security methods It has inbuilt security known as WS Security. REST inherits the security measures from the underlying transport protocol it is using like SOAP, etc.
Data Formats Permitted Data format permitted are only XML format. It permits data formats like XML, JSON, Plain test, HTML, etc.
SoapUI Tutorial Series

⇓ Subscribe Us ⇓


If you are not regular reader of this website then highly recommends you to Sign up for our free email newsletter!! Sign up just providing your email address below:


 

Check email in your inbox for confirmation to get latest updates Software Testing for free.


  Happy Testing!!!
 

2 thoughts on “REST Sample Project In SoapUI”

  1. What do you mean, no cons? I can think of one: SOAP has a WSDL that if properly constructed will self-document the service! I don’t need to write my own classes to deserialize objects, I have tools that’ll parse the WSDL and create those for me…

    With REST, I’m totally dependent on the API Developer(s) writing proper documentation. If the docs don’t match the API (trust me, it happens more often than not!), as a consumer of said API, I’m boned!

    Reply

Leave a Comment

Share This Post