Create RESTful Web Service from JAVA Class in SOA Suite 12C

Introduction

A web service is a collection of standards for exchanging data between applications. The purpose of the service is to provide the facility to its client so that client can access the resources in the system or in the applications. RESTful web service is based on REST (Representational State Transfer) architecture. RESTful web service provides resource representation or access to the resource through JSON and sets of HTTP methods. The following list describes some HTTP methods that are used by REST.

HTTP Method Operation Operation Type
GET Query a resource. Read Only
PUT Insert a new resource or update if the resource already exists. Idempotent
POST Update an existing resource. N/A
DELETE Delete a resource. Idempotent
OPTIONS List the supported operations on a resource. Read Only
HEAD Return only the response headers and no response body. Read Only

 

Properties of REST web service

  • REST messages are self explaining.
  • REST web services are stateless. Every interaction with a resource is stateless. The server does not monitor the status of its client.
  • Resource can be identified by URI. The purpose of an URI is to locate the resources on the server hosting the web service.
  • Caching is supported.

Advantages of RESTful web service

  • REST is a great way of developing lightweight Web services that are easy to implement and maintain.
  • It can improve performance by caching web service request results at some point between the service and the consumer.
  • Scalability level is high in case of RESTful web service as it is stateless
  • Lighter on bandwidth

Difference between SOAP based web service and REST web service

  • SOAP is protocol. REST is anA�architectural style.
  • SOAPA�requires more bandwidthand resource than REST. RESTA�requires less bandwidthA�and resource than SOAP.
  • SOAPA�defines its own security. RESTful web serviceA�inherits security measuresfrom the underlying transport.
  • SOAPA�permits XMLdata format only. RESTA�permits differentA�data format such as Plain text, HTML, XML, JSON etc.

Example of RESTful web service

Here I am going to explain how to create RESTful web service from a java class

  1. Create a Project under XXTestWSApp. Right click on the application and select New -> ProjectRESTful_WebService_1
  2. Select Custom Project under General categories. Enter project name XXRestWSPrjRESTful_WebService_2
  3. Create java class. Right click on the project and select java -> class. Enter the details as shown in the below screenshotRESTful_WebService_3
    1. Enter the below method in the class

    public String sayHello(String name){

    return “Hello “+name+”!”;

    }RESTful_WebService_4

  4. Right click on the java class and select Create RESTful ServiceRESTful_WebService_5
  5. Select JAX-RS 2.0 Style as Deployment platformRESTful_WebService_6
  6. Configure HTTP method in the next step. Select GET as operation typeRESTful_WebService_7
  7. Select application/xml as ProducesRESTful_WebService_8
  8. Select QueryParam as AnnotationRESTful_WebService_9
  9. Now HTTP method creation window will look like below screenshotRESTful_WebService_10
  10. Click on Finish and RESTful web service will be createdRESTful_WebService_11
  11. Right click on java class and select Test Web ServiceRESTful_WebService_12
  12. Enter Raja in the request parameter and click on Send Request. Response will come as expected Hello raja ! You can see afeature of RESTful web service here. Input string is attached in the URL (name=Raja)RESTful_WebService_13
  13. Try to test this web service in SOAP-UI also. Copy the URL and create a Rest project in the SOAP-UIRESTful_WebService_14
  14. Enter the copied URI and change the input string (name=leena).RESTful_WebService_15
  15. Now see the magic. Output will be different (Hello leena !). Here resource is identified through URI.RESTful_WebService_16

To make this web service available for the user, we need to deploy this project as WAR file. Please refer this link to follow the deployment step.

Leave a Reply

Your email address will not be published. Required fields are marked *

Show my latest post here