Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Web Development

SOA, Web Services, and RESTful Systems


Building RESTful Systems

If you eliminate typical web-service protocols (XML-RPC SOAP, WSDL, and so on), then how do you build an SOA-based RESTful system? With REST, you use that same mechanism used to request a web page—the HTTP query URL. For instance, the sample SOAP call in Example 1 makes a request for an employee's benefits information from a human resources web service.


<SOAP-ENV:Envelope xmlns:SOAP
 ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header> 
        some data here...    
    </SOAP-ENV:Header> 
    <SOAP-ENV:Body>
    <GetBenefits>
        <user>123-45-6789</user> 
        <type>full_time_employee</type> 
    </GetBenefits>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Example 1: Sample SOAP call to retrieve employment benefits for an employee.

With REST, you simply replace a SOAP call, such as Example 1, with the URL http://humanresources.com/benefits?user=<USER_SSID>&type=full_time_employee.

The HTTP query URL definition is all you need to know and use to make calls to a RESTful service. The response can be HTML, comma-delimited data, XML, or a more sophisticated document type (such as a spreadsheet). Some claim that the return of anything but hypermedia-based content is not truly RESTful. However, as long as the system stays true to the REST principals for the request and the communication protocol, the response type is unimportant.

When you build a web application with a Java Servlet, for example, it's straightforward to read the data passed through URL query parameters, and to return any text-based response to the caller. The Java Servlet doPost method implementation in Listing One illustrates this. Here, the parameters used in the query in Example 1 are read and used to retrieve a user's employee benefits. The results are encoded as human-readable text. Because this is an example of a RESTful service, the request can be initiated—and the response viewed—by web browsers, or any component in a distributed application.

protected void doPost( HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException
{
    ServletOutputStream out = resp.getOutputStream();
    String response;

    String userSSID = req.getParameter("user");
    String userType = req.getParameter("type");
    if ( userType.equals("full_time_employee")) {
        Employee emp = lookupUser(userSSID);
        String medPlan = emp.getMedicalPlan();
        String dntPlan = emp.getDentalPlan();
        String retPlan = emp.getRetirementPlan();
        Response = "User " + emp.getFullName() +
                   " has medical plan: " + medPlan +
                   ", and dental plan: " + dntPlan +
                   ", and retirement plan: " + retPlan;
    }
    else {
        // ...
    }
    // Output the response from the worker
    out.println(response);
}
Listing One


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.