Introduction | The Application | Building the Application
This is the fourth article in a series of articles on GlassFish and MySQL. Part 1 of the series describes the advantages of using GlassFish with MySQL and illustrates why the combination is a perfect choice for developing and deploying web applications. Part 2 shows how to develop a create, read, update, and delete (CRUD) web application that uses GlassFish and MySQL. The application uses the Java Persistence API implemented in GlassFish to manage data persistence. Part 3 shows how easy it is to convert the controller layer of the web application, that is, the layer of the application that performs the CRUD operations -- into a SOAP-based web service. It also shows how to create a client for the web service. In Part 4, you'll learn how to create a RESTful web service for the web application. You'll also examine a JavaFX client for the RESTful web service. As was the case for Part 3, the web service discussed in Part 4 uses GlassFish, MySQL, and the Java Persistence API.
This article shows you how to use the NetBeans IDE with GlassFish and MySQL to create the RESTful web service. Specifically, you'll take advantage of features in NetBeans IDE 6.5, GlassFish Server v2.1, and MySQL 5.1 Community Server to build and deploy the RESTful web service. This article also shows you how to use the NetBeans IDE with JavaFX support to run a JavaFX client for the web service. Contents
RESTful web services are web services that are built using the Representational State Transfer (REST) style of software architecture. An important concept in REST is the existence of resources, each of which can be referred to using a universal resource identifier (URI). In order to manipulate these resources, clients and servers communicate using a standardized interface such as HTTP and exchange representations of these resources. Because of the lightweight nature of REST and its ability to transmit data directly over HTTP, developers are increasingly choosing to build web services with the RESTful approach rather than using SOAP-based technologies. A RESTful web service is based on the following principles:
GlassFish, an open-source, enterprise-quality, Java EE 5-compliant application server, implements a set of Java technologies that simplify development and use of web services. This set or "stack" of technologies is collectively called Metro. Metro includes web service technologies such as Java API for XML-Based Web Services (JAX-WS) that enable you to develop and use SOAP-based web services. Part 3 of this series of articles describes how to develop a SOAP-based web service using JAX-WS. GlassFish also supports the Java API for RESTful Web Services (JAX-RS) and its production-ready reference implementation, Jersey, that you can use to develop and use RESTful web services. You can use the GlassFish Update Center to add Jersey to GlassFish.
JAX-RS provides a standardized API for Java developers to build RESTful web services. The API basically provides a set of annotations and associated classes and interfaces. Applying the annotations to Plain Old Java Objects (POJOs) enables you to expose web resources. Here, for example, is a class that uses JAX-RS annotations to expose a resource that represents a simple message:
The To communicate with a resource in REST, you make an HTTP request specifying its URI and one of the following HTTP methods:
In response, the Note that Jersey also provides a client API that you can use to consume a RESTful web service. However, the focus in this article is on the JavaFX approach to accessing a RESTful web service.
Java FX is a platform for creating and delivering rich Internet applications (RIAs) -- web applications that use rich media types such as video, audio, and graphics -- that can run in a wide variety of devices, anywhere from handsets to laptops to desktops. In many cases, you can compile and run the same JavaFX application code in a mobile environment, a desktop environment, or in a browser. The Java FX platform combines a declarative scripting language, JavaFX Script, and set of tools that enable developers and designers to build RIAs quickly and collaboratively. The tools include NetBeans IDE 6.5 for JavaFX 1.1 and JavaFX 1.1 Production Suite. NetBeans IDE 6.5 for JavaFX 1.1 provides a sophisticated integrated development environment (IDE) for building, previewing, and debugging JavaFX applications. A JavaFX plugin is also available to add the JavaFX platform to NetBeans IDE 6.5 if the IDE is already installed. NetBeans IDE 6.5 for JavaFX 1.1 features a drag-and-drop palette that you can use to quickly add JavaFX objects -- including objects for transformations, effects, and animation -- to application source code. The IDE automatically generates the source code for these objects. NetBeans IDE 6.5 for JavaFX 1.1 also comes with its own set of sample applications and the JavaFX Mobile Emulator, a mobile phone simulator. Figure 1 shows a JavaFX application in NetBeans IDE 6.5 for JavaFX 1.1. Notice the project structure in the Project window of the IDE, the source code in the editor window, and the drag-and-drop palette. Figure 1 also shows the output displayed by the running the application on the desktop and in the JavaFX Mobile Emulator.
JavaFX 1.1 Production Suite is a suite of tools and plugins that enable designers to export graphical assets from tools such as Adobe Photoshop and Adobe Illustrator to JavaFX applications. NetBeans IDE 6.5 for JavaFX 1.1 integrates JavaFX technology into NetBeans IDE 6.5 through the JavaFX 1.1 SDK. The SDK includes the JavaFX 1.1 desktop runtime, APIs, API documentation, compiler, sample programs, and the JavaFX 1.1 mobile runtime and Mobile Emulator. The SDK is also available as a stand-alone download. In other words, you can create JavaFX applications using NetBeans IDE 6.5 for JavaFX 1.1, or if you prefer to work from a command line or code editor, you can create JavaFX applications using the JavaFX 1.1 SDK. You can also create JavaFX applications using an IDE other than NetBeans by incorporating the SDK.
The declarative style of the JavaFX Script language makes it easy to program in a visual context. It allows you to create highly expressive user interfaces (UIs) quickly and easily. For example, the following JavaFX application coded in JavaFX Script renders a green rounded rectangle and a white circle with a red outline on the top of the rectangle. Both objects are placed on a window titled "Declaring Is Easy!"
Figure 2 shows what the application displays.
Notice how few lines of code are needed for the application. Also notice how the structure of the code models the UI. In the code, Variables: A variable is a construct that holds a value. You declare a variable with either the
You can also specify variables and values within an object instance and in this way define the state of the object. For example, in the previous circle-above-rectangle example, the
The You can perform transformations such as rotation on nodes. You can also animate nodes and apply various effects on them such as fading in or fading out. The Functions: A function is an executable block of code that performs some particular task. For example, the following function adds two numbers and prints the result:
The definition of a function begins with the
You can also specify a function within an object instance and in this way define behavior for the object. For example, suppose you want the circle-above-rectangle application to close when a user clicks the mouse pointer on the circle. You could do that by assigning a function to an instance variable in the
Here the Binding: One of the significant features in JavaFX Script is data binding. This features allows you bind the value of a variable with the value of some other construct such as another variable or an object. JavaFX Script calls this other construct a bound expression. What's important about this is that when the value of the variable changes, the new value is automatically assigned to the bound expression. For example, the following code displays the same result as shown in Figure 2.
However, if you change the value assigned to the
Libraries: JavaFX provides a rich set of libraries that makes it easy to include graphics, media, and web services in an application. The JavaFX application that displays the red-outlined circle above the green rectangle uses the JavaFX libraries shown in Table 2.
You can view these and additional JavaFX libraries in the JavaFX 1.1 API. You can also use any Java library in a JavaFX application. In other words, you can take advantage of JavaFX libraries to do things such as add rich media types to an application, perform animation, do transformations, or apply effects, and you can take advantage of features in the Java platform such as its sophisticated security architecture. » Continue to the next page of this article Introduction | The Application | Building the Application |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||