Chapter 5. Workshops

5.1. RESTEasy
5.2. Seam
Follow the instructions in these workshops to broaden your knowledge and understanding of JBoss Developer Studio.

5.1. RESTEasy

In this RESTEasy workshop we will provide an example of how to solve the problem of creating new customers for an online store. This will be achieved through the creation of a shopping application and adding customer records through a web browser.
Prerequisites
The following technologies are necessary for this workshop:
Make sure JBoss Developer Studio is open with the application server running. For simplicity it will be assumed for the rest of the workshop that you are running the JBoss Enterprise Application Platform 5, however the steps will be the same if you are using the JBoss Service Orientated Architecture Platform 5.
In JBoss Developer Studio navigate to HelpProject Example.
The Project Example menu being selected through the JBoss Developer Studio GUI.
Figure 5.1. Project Example Menu


In the Project Example menu dialog box, scroll to RESTEasyRESTEasy Simple Example and click the Finish button.
Selecting to create the RESTEasy Simple Example.
Figure 5.2. RESTEasy Simple Example


There may be two issues to fix before you can continue. The description for these issues are Target runtime JBoss 4.2.2 Runtime is not defined and Java compiler level does not match the version of the installed Java project facet. If they do not appear skip to Figure 5.7, “Completed fixing the issues”. If they do appear, the following steps will resolve these issues.
Displaying the two issues that will need to be fixed; detailed after this image.
Figure 5.3. Quick Fixes


To fix the first issue with the description Target runtime JBoss 4.2.2 Runtime is not defined click on this description and then click the Quick Fix button.
The description of the first issue is highlighted, waiting for the user to click the Quick Fix button.
Figure 5.4. Fixing the first issue


The next dialog box will display two runtime issues to fix, however by fixing the second one, labeled as Configure Targeted Runtimes, both issues will be corrected.
Select the issue called Configure Targeted Runtimes and click the Finish button.
Outlining the two runtime issues.
Figure 5.5. Runtime issues


A large dialog box will open displaying a long list of property categories on the left hand side. Navigate down to and click on the Targeted Runtimes property title. To the right of the menu where the JBoss 4.2.2 Runtime box is selected, deselect it and then select the name of your JBoss Enterprise Application Platform 5 runtime.
Once the JBoss Enterprise Application Platform 5 is selected and the old runtime deselected, click the Apply button and then the OK button.

Note

In Figure 5.6, “Selecting the correct runtime”, the runtime that will be used is the one called JBoss EAP 5.0 Runtime Server, though the name will depend on what you called it when you configured the server for use with JBoss Developer Studio.
The JBoss 4.2.2 Runtime is displayed as selected to show how the user will be greeted by this property window.
Figure 5.6. Selecting the correct runtime


To fix the second issue with the description Java compiler level does not match the version of the installed Java project facet, click on its description and then click the Quick Fix button.
After clicking the Quick Fix button the Java compiler issue should disappear because the JBoss Developer Studio has made the necessary changes in the background to fix it.
The Quick Fix dialog box should now be empty. Click the Finish button.
The Quick Fix dialog box is displayed without any remaining issues.
Figure 5.7. Completed fixing the issues


To add the project to the JBoss Enterprise Application Platform 5 server right click (or Control-click on Mac OS) on the server name in the Servers view in the bottom section of your workbench. Click on the Add and Remove... option.
The servers right-click menu is displayed with the Add and Remove option highlighted for selection.
Figure 5.8. About to add the project to the server


From the Add and Remove dialog box ensure the box labeled If server is started, publish changes immediately is ticked. In the left-hand menu, highlight the project you just created called simple by clicking on it.
The simple project is highlighted about the Add button about to be pressed to configure it for the server.
Figure 5.9. Add project to server


Click the Add > button to move it from the left-hand menu labeled Available to the right-hand menu labeled Configured. Once this is completed click the Finish button.
Your RESTEasy project has now been deployed onto your server. We will now test the application by using the REST Client plug-in within the Firefox web browser.
Open your Firefox web browser and navigate to ToolsREST Client.
Selecting the REST Client from the Firefox menu.
Figure 5.10. Selecting the REST Client


In the REST Client click on the Add Request Header button and type content-type for the Name and application/xml for the Value. This will ensure that the content will be consumable by the Plain Old Java Object (POJO) service at the server.
Select POST from the Method drop-down menu, copy http://localhost:8080/simple/rest-services/customers into the drop-down menu beside the Method and paste the following into the Request Body:
<customer><first-name>Bill</first-name><last-name>Burke</last-name><street>256 Clarendon Street</street><city>Boston</city><state>MA</state><zip>02115</zip><country>USA</country></customer>
The XML for the Request Body must not contain any spaces between XML tags to avoid exceptions occurring at the server end.
Click the Send button to add the customer information.
REST Client displays a green bar in the Response Header to signify a successful customer addition.
Figure 5.11. Adding a customer


To ensure the customer details were added, change the Method type to GET and the URL address to http://localhost:8080/simple/rest-services/customers/1. The /1 is added to the URL to select the customer with the customer id of 1, since that is the number that was given to the customer.
Click the Send button to retrieve the information. Select the Response Body tab beneath the Request Body section to view the returned information.
The customer information is displayed in the Response Body tab of the REST Client.
Figure 5.12. Retrieving customer information


We have added a customer and retrieved their information; now we will update their information. To achieve this change the Method to PUT and copy the following into the Request Body (overwriting anything that may be in the Request Body already):
<customer><first-name>Gary</first-name><last-name>Lamperillo</last-name><street>256 Clarendon Street</street><city>Venice</city><state>CA</state><zip>90291</zip><country>USA</country></customer>
Remember to make sure there are no spaces between the XML tags when you copy the information into the Request Body.
Check to make sure the URL still reads http://localhost:8080/simple/rest-services/customers/1 from when you retrieved the customer information. By using the URL that references the ID of the customer you are updating that customer's record.
Click the Send button to update the information.
REST Client displays a green bar in the Response Header to signify a successful update.
Figure 5.13. Updating customer information


To verify that the record has been updated change the Method type to GET, ensure the URL still references customer ID 1 and click the Send button.
Select the Response Body tab beneath the Request Body section to view the returned information.
REST Client displays a green bar in the Response Header to signify a successful update.
Figure 5.14. Retrieving updated customer information


The RESTEasy workshop is now complete.